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() {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(this.file, true))) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(this.file, false))) {
bufferedWriter.write("");
}
catch (IOException e) {

View File

@ -1,59 +1,50 @@
package persistence;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import persistence.category.CategoryEntity;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
class GenericCSVDAOTest {
GenericCSVDAO<CategoryEntity> sut;
public void beforeEachDirty() throws IOException {
File file = new File("test.csv");
if (file.exists()){
@BeforeEach
public void beforeEach() throws IOException {
if (file.exists()) {
file.delete();
}
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 {
beforeEachDirty();
assertEquals(0, sut.getALl().size());
var entityToAdd = new CategoryEntity("categoryName",99);
var entityToAdd = new CategoryEntity("categoryName", 99);
sut.add(entityToAdd);
var foundEntity = sut.getALl().stream().findFirst().orElseThrow();
assertEquals(entityToAdd,foundEntity);
assertEquals(entityToAdd, foundEntity);
}
@Test
public void removeWorks() throws IOException {
beforeEachDirty();
var entityToAdd = new CategoryEntity("categoryName",99);
var entityToAdd = new CategoryEntity("categoryName", 99);
sut.add(entityToAdd);
sut.remove(entityToAdd);
@ -63,13 +54,9 @@ class GenericCSVDAOTest {
@Test
public void removeAllWorks() throws IOException {
beforeEachDirty();
var entityToAdd1 = new CategoryEntity("categoryName1",101);
var entityToAdd1 = new CategoryEntity("categoryName1", 101);
sut.add(entityToAdd1);
var entityToAdd2 = new CategoryEntity("categoryName2",102);
var entityToAdd2 = new CategoryEntity("categoryName2", 102);
sut.add(entityToAdd2);
assertEquals(2, sut.getALl().size());
sut.removeAll();