fix: Made GardenPlanModelTest independent from json content.

isolated GardenPlanModelTests by pre-copying files. Tests pass now.
This commit is contained in:
David Guler 2022-11-15 11:28:48 +01:00
parent 09e582b8a2
commit 05e7bcc2e8
1 changed files with 26 additions and 12 deletions

View File

@ -6,17 +6,25 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.LocalDate;
import java.time.MonthDay;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.*;
public class GardenPlanModelTest {
private final URL dbDataSource = JsonCropList.class.getResource("user-crops.json");
private final URL testFile = JsonCropList.class.getResource("test-user-crops.json");
CropList cropList;
List<Crop> exampleCrops;
Crop exampleCropOnion;
@ -29,7 +37,7 @@ public class GardenPlanModelTest {
Garden model;
@BeforeEach
void setUp() throws IOException {
void setUp() throws IOException, URISyntaxException {
examplePlantOnion = new Plant(
@ -76,18 +84,24 @@ public class GardenPlanModelTest {
exampleCrops.add(exampleCrop1);
exampleCrops.add(exampleCrop2);
exampleCrops.add(exampleCrop3);
cropList = mockGardenPlan(exampleCrops);
cropList = mockCropList(exampleCrops);
GardenSchedule gardenSchedule = new GardenSchedule(new JsonTaskList(), new JsonPlantList());
model = new Garden(gardenSchedule, cropList);
// Reset Crop "database" before test
assertNotNull(testFile);
assertNotNull(dbDataSource);
Files.copy(Path.of(testFile.toURI()), Path.of(dbDataSource.toURI()), StandardCopyOption.REPLACE_EXISTING);
CropList testDatabase = new JsonCropList(dbDataSource);
GardenSchedule gardenSchedule = mock(GardenSchedule.class);
model = new Garden(gardenSchedule, testDatabase);
}
CropList mockGardenPlan(List<Crop> cropList) throws IOException {
CropList gardenPlan = mock(CropList.class);
when(gardenPlan.getCrops()).thenReturn(cropList);
when(gardenPlan.getCropById(5)).thenReturn(java.util.Optional.ofNullable(exampleCropCarrot));
when(gardenPlan.getCropById(3)).thenReturn(java.util.Optional.ofNullable(exampleCropOnion));
return gardenPlan;
CropList mockCropList(List<Crop> cropList) throws IOException {
CropList croplist = mock(CropList.class);
when(croplist.getCrops()).thenReturn(cropList);
when(croplist.getCropById(5)).thenReturn(java.util.Optional.ofNullable(exampleCropCarrot));
when(croplist.getCropById(3)).thenReturn(java.util.Optional.ofNullable(exampleCropOnion));
return croplist;
}
@Test