From 1dc2ad17749b38745f5e6d48e7bd92e18bc9da62 Mon Sep 17 00:00:00 2001 From: Elias Csomor Date: Sat, 26 Nov 2022 14:30:16 +0100 Subject: [PATCH] Extended testcoverage for classes --- .../ch/zhaw/gartenverwaltung/io/JsonCropListTest.java | 9 +++++++++ .../ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java | 3 +++ .../zhaw/gartenverwaltung/models/GardenScheduleTest.java | 1 + .../gartenverwaltung/io/corrupt-template-user-crops.json | 7 +++++++ 4 files changed, 20 insertions(+) create mode 100644 src/test/resources/ch/zhaw/gartenverwaltung/io/corrupt-template-user-crops.json diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonCropListTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonCropListTest.java index 87763a8..2913ea0 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonCropListTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonCropListTest.java @@ -30,6 +30,7 @@ public class JsonCropListTest { */ private final URL dbDataSource = this.getClass().getResource("test-user-crops.json"); private final URL testFile = this.getClass().getResource("template-user-crops.json"); + private final URL corruptTestFile = this.getClass().getResource("corrupt-template-user-crops.json"); @BeforeEach void connectToDb() throws URISyntaxException, IOException { @@ -101,5 +102,13 @@ public class JsonCropListTest { throw new RuntimeException(e); } } + + @Test + void detectCorruptJsonResource(){ + JsonCropList tL = new JsonCropList(corruptTestFile); + Assertions.assertThrows(InvalidJsonException.class, () -> { + tL.getCrops(); + }); + } } diff --git a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java index 4ceeb25..161ca47 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/io/JsonPlantListTest.java @@ -15,6 +15,7 @@ import java.util.Optional; import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; public class JsonPlantListTest { private final URL testFile = this.getClass().getResource("test-plantdb.json"); @@ -62,6 +63,8 @@ public class JsonPlantListTest { void getPlantByIdAndZone() { Optional testPlant; try { + assertThrows(HardinessZoneNotSetException.class, () -> testDatabase.getPlantById(null,1).get()); + testPlant = testDatabase.getPlantById(HardinessZone.ZONE_8A, 1); } catch (IOException | HardinessZoneNotSetException e) { throw new RuntimeException(e); diff --git a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java index 5e32687..0c05e36 100644 --- a/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java +++ b/src/test/java/ch/zhaw/gartenverwaltung/models/GardenScheduleTest.java @@ -188,6 +188,7 @@ class GardenScheduleTest { @Test void planTasksForCrop() throws HardinessZoneNotSetException, PlantNotFoundException, IOException { + assertThrows(PlantNotFoundException.class,()-> model.planTasksForCrop(new Crop())); model.planTasksForCrop(new Crop(20, exampleStartDate).withId(30)); verify(exampleTaskTemplateList.get(0), times(1)).generateTask(exampleStartDate, 30); verify(exampleTaskTemplateList.get(1), times(1)).generateTask(exampleStartDate, 30); diff --git a/src/test/resources/ch/zhaw/gartenverwaltung/io/corrupt-template-user-crops.json b/src/test/resources/ch/zhaw/gartenverwaltung/io/corrupt-template-user-crops.json new file mode 100644 index 0000000..73bf592 --- /dev/null +++ b/src/test/resources/ch/zhaw/gartenverwaltung/io/corrupt-template-user-crops.json @@ -0,0 +1,7 @@ +[ + { + "plantId": 1, + "startDate": "2023-02-25", + "area": 0.5 + } +] \ No newline at end of file