From 7da4a7770c3bea84cdfdfdfea6abbbb76e9b0a72 Mon Sep 17 00:00:00 2001 From: Rhys Evans <EvansRM17@cardiff.ac.uk> Date: Mon, 11 Dec 2023 21:01:30 +0000 Subject: [PATCH] Further testing of tests. Issues with calling JDBC databse to a test environment --- build.gradle | 12 +- .../java/Team5/SmartTowns/Data/Location.java | 5 + .../java/Team5/SmartTowns/CalculatorTest.java | 104 +++++++++--------- .../Team5/SmartTowns/Data/LocationTest.java | 7 ++ .../Data/locationRepositoryJDBCTest.java | 46 ++++++++ src/test/java/Team5/SmartTowns/Test5.java | 15 ++- src/test/java/Team5/SmartTowns/Test6Mock.java | 79 +++++++++++++ 7 files changed, 206 insertions(+), 62 deletions(-) create mode 100644 src/test/java/Team5/SmartTowns/Data/LocationTest.java create mode 100644 src/test/java/Team5/SmartTowns/Data/locationRepositoryJDBCTest.java create mode 100644 src/test/java/Team5/SmartTowns/Test6Mock.java diff --git a/build.gradle b/build.gradle index 33328045..5689fe1a 100644 --- a/build.gradle +++ b/build.gradle @@ -37,12 +37,12 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-validation' // https://mvnrepository.com/artifact/org.springframework/spring-dao // implementation group: 'org.springframework', name: 'spring-tx', version: '6.1.1' - implementation 'org.springframework:spring-dao:2.0.8' +// implementation 'org.springframework:spring-dao:2.0.8' // implementation 'org.springframework.dao.DataAccessException' // https://mvnrepository.com/artifact/org.springframework/spring-core - implementation 'org.springframework:spring-core:6.0.6' - implementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.1' - implementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.1' +// implementation 'org.springframework:spring-core:6.0.6' +// implementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.1' +// implementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.1' //Thanks for using https://jar-download.com @@ -58,3 +58,7 @@ tasks.named('test') { useJUnitPlatform() } +tasks.named('test') { + Test6Mock() +} + diff --git a/src/main/java/Team5/SmartTowns/Data/Location.java b/src/main/java/Team5/SmartTowns/Data/Location.java index cf9e6964..7ec4ede3 100644 --- a/src/main/java/Team5/SmartTowns/Data/Location.java +++ b/src/main/java/Team5/SmartTowns/Data/Location.java @@ -50,4 +50,9 @@ public class Location { } + + public int testMe(){ + int a=4; + return 4; + } } diff --git a/src/test/java/Team5/SmartTowns/CalculatorTest.java b/src/test/java/Team5/SmartTowns/CalculatorTest.java index 49fc027f..1aea1c6a 100644 --- a/src/test/java/Team5/SmartTowns/CalculatorTest.java +++ b/src/test/java/Team5/SmartTowns/CalculatorTest.java @@ -1,53 +1,53 @@ -//package Team5.SmartTowns; -// -//import Team5.SmartTowns.Data.Calculator; -//import org.junit.jupiter.api.BeforeAll; -//import org.junit.jupiter.api.Test; -//import org.springframework.boot.test.context.SpringBootTest; -//import org.springframework.core.NestedRuntimeException; -////import org.springframework.dao.DataAccessException; -// -//import static org.junit.jupiter.api.Assertions.assertEquals; -//import static org.junit.jupiter.api.Assertions.assertThrows; -//import org.junit.platform.engine.TestDescriptor; +package Team5.SmartTowns; + +import Team5.SmartTowns.Data.Calculator; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.NestedRuntimeException; +//import org.springframework.dao.DataAccessException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.platform.engine.TestDescriptor; //import org.junit.platform.commons.util.Preconditions; -//import org.apache.commons.logging.LogFactory; -//@SpringBootTest -//public class CalculatorTest { -//// String asd= getLegacyReportingName("das"); -// -// private static Calculator calculator; -// -// @BeforeAll -// public static void before() { -// calculator = new Calculator(); -// } -// -// @Test -// public void whenDivideNumbersThenReturnDivision() { -// -// assertEquals(2, calculator.divide(4, 2)); -// } -// -// @Test -// public void whenDivideByZeroThenThrowException() { -// assertThrows(ArithmeticException.class, () -> calculator.divide(1, 0)); -// } -// @Test -// public void whenAddingThingsThenAdd(){ -// assertEquals(10,calculator.add(6,4)); -// } -// @Test -// public void whenSubtractingThingsThenSubtract(){ -// assertEquals(-5,calculator.subtract(5,10)); -// } -// -// @Test -// public void whenMultiplyingThingsThenMultiply(){ -// assertEquals(-60,calculator.multiply(-3,20)); -// } -// -// -// -// -//} +import org.apache.commons.logging.LogFactory; +@SpringBootTest +public class CalculatorTest { +// String asd= getLegacyReportingName("das"); + + private static Calculator calculator; + + @BeforeAll + public static void before() { + calculator = new Calculator(); + } + + @Test + public void whenDivideNumbersThenReturnDivision() { + + assertEquals(2, calculator.divide(4, 2)); + } + + @Test + public void whenDivideByZeroThenThrowException() { + assertThrows(ArithmeticException.class, () -> calculator.divide(1, 0)); + } + @Test + public void whenAddingThingsThenAdd(){ + assertEquals(10,calculator.add(6,4)); + } + @Test + public void whenSubtractingThingsThenSubtract(){ + assertEquals(-5,calculator.subtract(5,10)); + } + + @Test + public void whenMultiplyingThingsThenMultiply(){ + assertEquals(-60,calculator.multiply(-3,20)); + } + + + + +} diff --git a/src/test/java/Team5/SmartTowns/Data/LocationTest.java b/src/test/java/Team5/SmartTowns/Data/LocationTest.java new file mode 100644 index 00000000..3607963a --- /dev/null +++ b/src/test/java/Team5/SmartTowns/Data/LocationTest.java @@ -0,0 +1,7 @@ +package Team5.SmartTowns.Data; + +import static org.junit.jupiter.api.Assertions.*; + +class LocationTest { + +} \ No newline at end of file diff --git a/src/test/java/Team5/SmartTowns/Data/locationRepositoryJDBCTest.java b/src/test/java/Team5/SmartTowns/Data/locationRepositoryJDBCTest.java new file mode 100644 index 00000000..29564125 --- /dev/null +++ b/src/test/java/Team5/SmartTowns/Data/locationRepositoryJDBCTest.java @@ -0,0 +1,46 @@ +//package Team5.SmartTowns.Data; +// +//import org.junit.jupiter.api.BeforeAll; +//import org.junit.jupiter.api.Test; +//import org.mockito.Mock; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.boot.test.context.SpringBootTest; +//import org.springframework.jdbc.core.JdbcTemplate; +// +//import java.util.List; +// +//import static org.junit.jupiter.api.Assertions.*; +//// create lists of sql data todo +//@SpringBootTest +//class locationRepositoryJDBCTest { +// +//private static locationRepository locationRepo; +// +// +// @Autowired +// private locationRepository locationRepo; +// +// @Mock +// private JdbcTemplate jdbc; +//@BeforeAll +//public static void setup(){ +// locationRepositoryJDBC aa= new locationRepositoryJDBC(jdbc); +//} +// @Test +// void getAllLocation() { +// List<Location> locations= locationRepo.getAllLocation(); +// int preAdd = locations.size(); +// Location loc = new Location("House","House@Bricks","Description Here","Caerphilly",103); +// locationRepo.addLocation(loc); +// int postAdd = locationRepo.getAllLocation().size(); +// assertEquals(1,(postAdd-preAdd)); +// } +// +// @Test +// void addLocation() { +// } +// +// @Test +// void setJdbc() { +// } +//} \ No newline at end of file diff --git a/src/test/java/Team5/SmartTowns/Test5.java b/src/test/java/Team5/SmartTowns/Test5.java index a5a518e5..4ea4af12 100644 --- a/src/test/java/Team5/SmartTowns/Test5.java +++ b/src/test/java/Team5/SmartTowns/Test5.java @@ -1,5 +1,6 @@ //package Team5.SmartTowns; // +//import Team5.SmartTowns.Data.locationRepository; //import Team5.SmartTowns.Data.locationRepositoryJDBC; ////import org.junit.jupiter.api.Test; //import org.springframework.beans.factory.annotation.Autowired; @@ -13,21 +14,23 @@ //import static org.testng.Assert.assertEquals; // //@SpringBootTest -//@JdbcTest -// -//@Sql({"schema.sql", "data.sql"}) +////@JdbcTest +//// +//@Sql({"schema.sql", "test-data.sql"}) //public class Test5 { // // @Autowired -// private JdbcTemplate ajdbc; +// private locationRepository locationRepo; // // // @Test //// test 1 // void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount() { -// locationRepositoryJDBC employeeDAO = new locationRepositoryJDBC(ajdbc); +//// locationRepositoryJDBC employeeDAO = new locationRepositoryJDBC(ajdbc); +// int aa=2; +// int bb=2; //// employeeDAO.setJdbc(ajdbc); -// assertEquals(4, employeeDAO.getAllLocation().size()); +// assertEquals(4, (aa+bb)); // // } // diff --git a/src/test/java/Team5/SmartTowns/Test6Mock.java b/src/test/java/Team5/SmartTowns/Test6Mock.java new file mode 100644 index 00000000..f84b318f --- /dev/null +++ b/src/test/java/Team5/SmartTowns/Test6Mock.java @@ -0,0 +1,79 @@ +//package Team5.SmartTowns; +// +//import Team5.SmartTowns.Data.Location; +//import Team5.SmartTowns.Data.locationRepository; +//import Team5.SmartTowns.Data.locationRepositoryJDBC; +//import org.junit.Before; +////import org.junit.runner.RunWith; +////import org.mockito.Mockito; +////import org.mockito.internal.invocation.InvocationsFinder; +//import org.junit.jupiter.api.BeforeAll; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.boot.test.context.SpringBootTest; +//import org.springframework.boot.test.context.TestConfiguration; +// +//import org.springframework.boot.test.mock.mockito.MockBean; +//import org.springframework.context.annotation.Bean; +//import org.springframework.jdbc.core.JdbcTemplate; +////import org.springframework.test.context.junit4.SpringRunner; +//import org.testng.annotations.Test; +// +//import java.util.ArrayList; +//import java.util.List; +// +//import static org.testng.AssertJUnit.assertEquals; +// +////@RunWith(SpringRunner.class) +//@SpringBootTest +//public class Test6Mock { +// +// @TestConfiguration +// static class EmployeeServiceImplTestContextConfiguration { +// +// @Bean +// public Location location() { +// return new Location(); +// } +// } +// +// +// @Autowired +// private locationRepository locationRepo; +// @Autowired +// private locationRepositoryJDBC locationRepoJ; +// +// +// @MockBean +// private JdbcTemplate jdbc; +// +// +// @BeforeAll +// public static void setUp() { +// locationRepository locationRepository= new locationRepository() { +// @Override +// public List<Location> getAllLocation() { +// return null; +// } +// +// @Override +// public void addLocation(Location loc) { +// +// } +// }; +// +//// Location alex = new Location("House","House@Bricks","Description Here","Caerphilly",103); +//// +//// Mockito.when(locationRepository.getAllLocation()) +//// .thenReturn(locationRepository.getAllLocation()); +// } +// +// +// @Test +// public void whenValidName_thenEmployeeShouldBeFound() { +// locationRepositoryJDBC s= new locationRepositoryJDBC(jdbc); +// List<Location> locations = locationRepository.getAllLocation(); +// assertEquals(18,locations.size()); +// +// } +// } +// -- GitLab