found a bug, this is why we write tests
continuous-integration/drone/push Build is passing Details

This commit is contained in:
qvalentin 2022-05-13 20:01:14 +02:00
parent f616b4b4bf
commit 45630b2813
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
2 changed files with 44 additions and 57 deletions

View File

@ -61,7 +61,7 @@ public class GenericCSVDAO<T extends CSVSerializable> implements GenericDAO<T> {
public void removeAll() { public void removeAll() {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(this.file, true))) { try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(this.file, false))) {
bufferedWriter.write(""); bufferedWriter.write("");
} }
catch (IOException e) { catch (IOException e) {

View File

@ -1,39 +1,38 @@
package persistence; package persistence;
import org.junit.Before; import org.junit.jupiter.api.AfterEach;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import persistence.category.CategoryEntity; import persistence.category.CategoryEntity;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
class GenericCSVDAOTest { class GenericCSVDAOTest {
GenericCSVDAO<CategoryEntity> sut; GenericCSVDAO<CategoryEntity> sut;
public void beforeEachDirty() throws IOException {
File file = new File("test.csv"); File file = new File("test.csv");
@BeforeEach
public void beforeEach() throws IOException {
if (file.exists()) { if (file.exists()) {
file.delete(); file.delete();
} }
file.createNewFile(); file.createNewFile();
this.sut = new GenericCSVDAO<CategoryEntity>(file,CategoryEntity::new); this.sut = new GenericCSVDAO<>(file, CategoryEntity::new);
}
@AfterEach
void afterEach() {
file.delete();
} }
@Test @Test
public void addWorks() throws IOException { public void addWorks() throws IOException {
beforeEachDirty();
assertEquals(0, sut.getALl().size()); assertEquals(0, sut.getALl().size());
var entityToAdd = new CategoryEntity("categoryName", 99); var entityToAdd = new CategoryEntity("categoryName", 99);
@ -41,18 +40,10 @@ class GenericCSVDAOTest {
var foundEntity = sut.getALl().stream().findFirst().orElseThrow(); var foundEntity = sut.getALl().stream().findFirst().orElseThrow();
assertEquals(entityToAdd, foundEntity); assertEquals(entityToAdd, foundEntity);
} }
@Test @Test
public void removeWorks() throws IOException { public void removeWorks() throws IOException {
beforeEachDirty();
var entityToAdd = new CategoryEntity("categoryName", 99); var entityToAdd = new CategoryEntity("categoryName", 99);
sut.add(entityToAdd); sut.add(entityToAdd);
@ -63,10 +54,6 @@ class GenericCSVDAOTest {
@Test @Test
public void removeAllWorks() throws IOException { public void removeAllWorks() throws IOException {
beforeEachDirty();
var entityToAdd1 = new CategoryEntity("categoryName1", 101); var entityToAdd1 = new CategoryEntity("categoryName1", 101);
sut.add(entityToAdd1); sut.add(entityToAdd1);
var entityToAdd2 = new CategoryEntity("categoryName2", 102); var entityToAdd2 = new CategoryEntity("categoryName2", 102);