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,79 +1,66 @@
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;
File file = new File("test.csv");
public void beforeEachDirty() throws IOException { @BeforeEach
File file = new File("test.csv"); 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
public void addWorks() throws IOException {
assertEquals(0, sut.getALl().size());
@Test var entityToAdd = new CategoryEntity("categoryName", 99);
public void addWorks() throws IOException { sut.add(entityToAdd);
var foundEntity = sut.getALl().stream().findFirst().orElseThrow();
beforeEachDirty(); assertEquals(entityToAdd, foundEntity);
}
@Test
public void removeWorks() throws IOException {
var entityToAdd = new CategoryEntity("categoryName", 99);
sut.add(entityToAdd);
assertEquals(0, sut.getALl().size()); sut.remove(entityToAdd);
var entityToAdd = new CategoryEntity("categoryName",99); assertEquals(0, sut.getALl().size());
sut.add(entityToAdd); }
var foundEntity = sut.getALl().stream().findFirst().orElseThrow();
assertEquals(entityToAdd,foundEntity); @Test
public void removeAllWorks() throws IOException {
var entityToAdd1 = new CategoryEntity("categoryName1", 101);
sut.add(entityToAdd1);
var entityToAdd2 = new CategoryEntity("categoryName2", 102);
sut.add(entityToAdd2);
assertEquals(2, sut.getALl().size());
sut.removeAll();
assertEquals(0, sut.getALl().size());
}
}
@Test
public void removeWorks() throws IOException {
beforeEachDirty();
var entityToAdd = new CategoryEntity("categoryName",99);
sut.add(entityToAdd);
sut.remove(entityToAdd);
assertEquals(0, sut.getALl().size());
}
@Test
public void removeAllWorks() throws IOException {
beforeEachDirty();
var entityToAdd1 = new CategoryEntity("categoryName1",101);
sut.add(entityToAdd1);
var entityToAdd2 = new CategoryEntity("categoryName2",102);
sut.add(entityToAdd2);
assertEquals(2, sut.getALl().size());
sut.removeAll();
assertEquals(0, sut.getALl().size());
}
} }