Skip to content
Snippets Groups Projects
Commit 8e3769f5 authored by Rhys Evans's avatar Rhys Evans
Browse files

Further work attempting to get JUnit test working :( :( :(

parent 694e717c
No related branches found
No related tags found
1 merge request!34Resolve "As a user, I would like a town specific page which shows all trails for that town so that I can easily see my progress"
......@@ -22,6 +22,7 @@ repositories {
}
dependencies {
implementation 'org.springframework:spring-dao:2.0.8'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
......@@ -34,7 +35,13 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework:spring-dao:2.0.8'
// https://mvnrepository.com/artifact/org.springframework/spring-dao
// https://mvnrepository.com/artifact/org.springframework/spring-dao
implementation group: 'org.springframework', name: 'spring-tx', version: '6.1.1'
// implementation 'org.springframework.dao.DataAccessException'
//Thanks for using https://jar-download.com
}
tasks.named('bootBuildImage') {
......
......@@ -5,6 +5,8 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import javax.print.DocFlavor;
import javax.sql.DataSource;
import java.util.List;
@Repository
......@@ -26,6 +28,14 @@ public class locationRepositoryJDBC implements locationRepository {
rs.getInt("locationTrailID")
);
}
public String getCountOfEmployees() {
return jdbc.queryForObject("SELECT COUNT(*) FROM locations",String.class);
}
public List<Location> getAllLocation2(JdbcTemplate aJdbc){
String sql= "SELECT * FROM locations";
return aJdbc.query(sql, locationMapper);
}
public List<Location> getAllLocation(){
String sql= "SELECT * FROM locations";
return jdbc.query(sql, locationMapper);
......@@ -39,4 +49,10 @@ public class locationRepositoryJDBC implements locationRepository {
}
public void setDataSource(DataSource dataSource) {
jdbc = new JdbcTemplate(dataSource);
}
public locationRepositoryJDBC() {
}
}
package Team5.SmartTowns;
import Team5.SmartTowns.Data.Location;
import Team5.SmartTowns.Data.Town;
import Team5.SmartTowns.Data.TownRepository;
import Team5.SmartTowns.Data.locationRepository;
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.data.jdbc.DataJdbcTest;
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.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.util.ReflectionTestUtils;
import org.testng.Assert;
import java.util.List;
import javax.sql.DataSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
......@@ -26,8 +22,44 @@ import static org.mockito.Mockito.mock;
@SpringBootTest
@JdbcTest
@Sql({"schema.sql", "data.sql"})
class SmartTownsApplicationTests {
@Autowired
private JdbcTemplate ajdbc;
@Test
// test2
public void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount1() {
DataSource dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
.addScript("classpath:jdbc/schema.sql")
.addScript("classpath:jdbc/test-data.sql")
.build();
locationRepositoryJDBC employeeDAO = new locationRepositoryJDBC();
employeeDAO.setDataSource(dataSource);
assertEquals(4, employeeDAO.getCountOfEmployees());}
@Test
// test 1
void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount() {
locationRepositoryJDBC employeeDAO = new locationRepositoryJDBC();
employeeDAO.setJdbcTemplate(ajdbc);
assertEquals(4, employeeDAO.getAllLocation().size());
// assertEquals(4, employeeDAO.getCountOfEmployees());
}
// @Test
// void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount() {
// EmployeeDAO employeeDAO = new EmployeeDAO();
// employeeDAO.setJdbcTemplate(jdbcTemplate);
//
// assertEquals(4, employeeDAO.getCountOfEmployees());
// }
//
// private static TownController townController;
......@@ -36,37 +68,37 @@ class SmartTownsApplicationTests {
// 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 static Team5.SmartTowns.Data.TownRepository townRepository;
// private Team5.SmartTowns.Data.locationRepository locationRepo;
// JdbcTemplate jdbc = Mockito.mock(JdbcTemplate.class);
//// @Autowired
//// private static Team5.SmartTowns.Data.TownRepository townRepository;
////
////
//// private static Team5.SmartTowns.Data.Location town;
//// private static RowMapper<Location> locationMapper;
//
//
// private static Team5.SmartTowns.Data.Location town;
// private static RowMapper<Location> locationMapper;
@BeforeAll
public void before() {
// MockitoAnnotations.initMocks(this);
// location = new Location();
locationRepo = mock(new locationRepositoryJDBC(jdbc));
// townController = new TownController();
}
// @BeforeAll
// public static void before(){}
// location = new Location();
//}
@Test
void contextLoads() {
}
@Test
public void whenFilteringTownsByLocationsReturnOneTown() {
List<Location> allLocations = ( locationRepo.getAllLocation());
// public void before() {
//// MockitoAnnotations.initMocks(this);
//// location = new Location();
// locationRepo = mock(new locationRepositoryJDBC(jdbc));
//// townController = new TownController();
// }
//// @BeforeAll
//// public static void before(){}
//// location = new Location();
////}
//
// @Test
// void contextLoads() {
// }
//
// @Test
// public void whenFilteringTownsByLocationsReturnOneTown() {
// List<Location> allLocations = ( locationRepo.getAllLocation());
// List<Town> allTowns = townRepository.getAllTowns();
// int allLocationNumber=allLocations.size();
// int allLocationNumberAfterFilter=0;
......
package Team5.SmartTowns;
import Team5.SmartTowns.Data.locationRepositoryJDBC;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Test3 {
@Test
// test2
public void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount1() {
DataSource dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
.addScript("classpath:jdbc/schema.sql")
.addScript("classpath:jdbc/test-data.sql")
.build();
locationRepositoryJDBC employeeDAO = new locationRepositoryJDBC();
employeeDAO.setDataSource(dataSource);
assertEquals(4, employeeDAO.getCountOfEmployees());}
}
package Team5.SmartTowns;
import Team5.SmartTowns.Data.locationRepositoryJDBC;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class Test4 {
@Mock
JdbcTemplate jdbcTemplate;
@Test
public void whenMockJdbcTemplate_thenReturnCorrectEmployeeCount() {
locationRepositoryJDBC employeeDAO = new locationRepositoryJDBC();
ReflectionTestUtils.setField(employeeDAO, "jdbcTemplate", jdbcTemplate);
Mockito.when(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM locations", Integer.class))
.thenReturn(4);
assertEquals(4, employeeDAO.getCountOfEmployees());
}
}
package Team5.SmartTowns;
import Team5.SmartTowns.Data.locationRepositoryJDBC;
import org.junit.jupiter.api.Test;
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.test.context.jdbc.Sql;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
@JdbcTest
@Sql({"schema.sql", "data.sql"})
public class Test5 {
@Autowired
private JdbcTemplate ajdbc;
@Test
// test 1
void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount() {
locationRepositoryJDBC employeeDAO = new locationRepositoryJDBC(ajdbc);
// employeeDAO.setJdbcTemplate(ajdbc);
assertEquals(4, employeeDAO.getAllLocation().size());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment