Merge pull request #8891 from wugangca/BAEL-3069
BAEL-3069 Spring jdbcTemplate Unit Testing
This commit is contained in:
commit
c04dad72c2
@ -72,6 +72,7 @@
|
||||
<module>spring-jpa</module>
|
||||
<!-- <module>spring-mybatis</module> --> <!-- needs fixing in BAEL-9021 -->
|
||||
<module>spring-persistence-simple</module>
|
||||
<module>spring-persistence-simple-2</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
60
persistence-modules/spring-persistence-simple-2/pom.xml
Normal file
60
persistence-modules/spring-persistence-simple-2/pom.xml
Normal file
@ -0,0 +1,60 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-persistence-simple-2</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-persistence-simple-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>persistence-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- persistence -->
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${org.springframework.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>5.2.4.RELEASE</org.springframework.version>
|
||||
<!-- persistence -->
|
||||
<h2.version>1.4.200</h2.version>
|
||||
<!-- test scoped -->
|
||||
<mockito.version>3.3.3</mockito.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,44 @@
|
||||
package com.baeldung.jdbc;
|
||||
|
||||
public class Employee {
|
||||
private int id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String address;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(final String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(final String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(final String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.jdbc;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class EmployeeDAO {
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
public int getCountOfEmployees() {
|
||||
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
CREATE TABLE EMPLOYEE
|
||||
(
|
||||
ID int NOT NULL PRIMARY KEY,
|
||||
FIRST_NAME varchar(255),
|
||||
LAST_NAME varchar(255),
|
||||
ADDRESS varchar(255)
|
||||
);
|
@ -0,0 +1,4 @@
|
||||
INSERT INTO EMPLOYEE VALUES (1, 'James', 'Gosling', 'Canada');
|
||||
INSERT INTO EMPLOYEE VALUES (2, 'Donald', 'Knuth', 'USA');
|
||||
INSERT INTO EMPLOYEE VALUES (3, 'Linus', 'Torvalds', 'Finland');
|
||||
INSERT INTO EMPLOYEE VALUES (4, 'Dennis', 'Ritchie', 'USA');
|
@ -0,0 +1,51 @@
|
||||
package com.baeldung.jdbc;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
|
||||
public class EmployeeDAOUnitTest {
|
||||
@Mock
|
||||
JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Test
|
||||
public void whenMockJdbcTemplate_thenReturnCorrectEmployeeCount() {
|
||||
EmployeeDAO employeeDAO = new EmployeeDAO();
|
||||
ReflectionTestUtils.setField(employeeDAO, "jdbcTemplate", jdbcTemplate);
|
||||
Mockito.when(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class))
|
||||
.thenReturn(4);
|
||||
|
||||
assertEquals(4, employeeDAO.getCountOfEmployees());
|
||||
|
||||
Mockito.when(jdbcTemplate.queryForObject(Mockito.anyString(), Mockito.eq(Integer.class)))
|
||||
.thenReturn(3);
|
||||
|
||||
assertEquals(3, employeeDAO.getCountOfEmployees());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount() {
|
||||
DataSource dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
|
||||
.addScript("classpath:jdbc/schema.sql")
|
||||
.addScript("classpath:jdbc/test-data.sql")
|
||||
.build();
|
||||
|
||||
EmployeeDAO employeeDAO = new EmployeeDAO();
|
||||
employeeDAO.setDataSource(dataSource);
|
||||
|
||||
assertEquals(4, employeeDAO.getCountOfEmployees());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user