From e321e10b4b8276ada786f33cbfc8051188924c8f Mon Sep 17 00:00:00 2001
From: Rhys Evans <EvansRM17@cardiff.ac.uk>
Date: Thu, 7 Dec 2023 12:28:40 +0000
Subject: [PATCH] Further work on getting JUnit test to work

---
 .../SmartTownsApplicationTests.java           | 41 +++++++++++++++----
 1 file changed, 32 insertions(+), 9 deletions(-)

diff --git a/src/test/java/Team5/SmartTowns/SmartTownsApplicationTests.java b/src/test/java/Team5/SmartTowns/SmartTownsApplicationTests.java
index 68a585a4..67932b97 100644
--- a/src/test/java/Team5/SmartTowns/SmartTownsApplicationTests.java
+++ b/src/test/java/Team5/SmartTowns/SmartTownsApplicationTests.java
@@ -3,35 +3,57 @@ package Team5.SmartTowns;
 import Team5.SmartTowns.Data.Location;
 import Team5.SmartTowns.Data.Town;
 import Team5.SmartTowns.Data.TownRepository;
+import Team5.SmartTowns.Data.locationRepositoryJDBC;
 import Team5.SmartTowns.Towns.TownController;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.RowMapper;
+import org.springframework.test.context.jdbc.Sql;
+import org.springframework.test.util.ReflectionTestUtils;
 
 import java.util.List;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.mockito.Mockito.mock;
 
 @SpringBootTest
+@JdbcTest
+@Sql({"schema.sql", "data.sql"})
 class SmartTownsApplicationTests {
 
 
 	private static TownController townController;
-	@Autowired
-	private Team5.SmartTowns.Data.locationRepository locationRepository;
 
-	private Team5.SmartTowns.Data.Location location;
 
+	private static Team5.SmartTowns.Data.Location location;
+//	@Mock
+//	private static JdbcTemplate jdbc;
+
+	@Autowired
+	private Team5.SmartTowns.Data.locationRepository locationRepo;
+	JdbcTemplate jdbc = Mockito.mock(JdbcTemplate.class);
 	@Autowired
-	private Team5.SmartTowns.Data.TownRepository townRepository;
+	private static Team5.SmartTowns.Data.TownRepository townRepository;
+
+
+	private static Team5.SmartTowns.Data.Location town;
+	private static RowMapper<Location> locationMapper;
 
-	private Team5.SmartTowns.Data.Location town;
 
 	@BeforeAll
-	public static void before() {
-		locationRepositroy = new location.locationRepository();
+	public void before() {
+//		MockitoAnnotations.initMocks(this);
+		location = new Location();
+		locationRepo = mock(new locationRepositoryJDBC(jdbc));
+		townController = new TownController();
 	}
 //	@BeforeAll
 //	public static void before(){}
@@ -44,12 +66,13 @@ class SmartTownsApplicationTests {
 
 	@Test
 	public void whenFilteringTownsByLocationsReturnOneTown() {
-		List<Location> allLocations = locationRepository.getAllLocation();
+		List<Location> allLocations = ( locationRepo.getAllLocation());
 		List<Town> allTowns = townRepository.getAllTowns();
 		int allLocationNumber=allLocations.size();
 		int allLocationNumberAfterFilter=0;
 		for (Town town : allTowns){
-			allLocationNumberAfterFilter+=townController.filterByLocationForTrails(allLocations,town.getTownName()).size();
+			allLocationNumberAfterFilter+=townController.filterByLocationForTrails(allLocations,
+					town.getTownName()).size();
 		}
 		assertSame(allLocationNumber,allLocationNumberAfterFilter);
  /// list of all locations,
-- 
GitLab