Skip to content

Commit fa4a343

Browse files
authored
Merge pull request eugenp#6891 from dev-chirag/master
BAEL-2848 JUnit Tagging and Filtering Tests
2 parents d3fc478 + 478e9f0 commit fa4a343

16 files changed

Lines changed: 537 additions & 0 deletions

testing-modules/junit-5-configuration/pom.xml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,55 @@
1616
</parent>
1717

1818
<dependencies>
19+
<dependency>
20+
<groupId>org.junit.platform</groupId>
21+
<artifactId>junit-platform-engine</artifactId>
22+
<version>${junit.platform.version}</version>
23+
</dependency>
1924
<dependency>
2025
<groupId>org.junit.jupiter</groupId>
2126
<artifactId>junit-jupiter-params</artifactId>
2227
<version>${junit.jupiter.version}</version>
2328
</dependency>
29+
<dependency>
30+
<groupId>org.junit.platform</groupId>
31+
<artifactId>junit-platform-runner</artifactId>
32+
<version>${junit.platform.version}</version>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.junit.vintage</groupId>
37+
<artifactId>junit-vintage-engine</artifactId>
38+
<version>${junit.vintage.version}</version>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.junit.jupiter</groupId>
43+
<artifactId>junit-jupiter-migrationsupport</artifactId>
44+
<version>${junit.vintage.version}</version>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.h2database</groupId>
49+
<artifactId>h2</artifactId>
50+
<version>${h2.version}</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework</groupId>
54+
<artifactId>spring-test</artifactId>
55+
<version>${spring.version}</version>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.springframework</groupId>
60+
<artifactId>spring-context</artifactId>
61+
<version>${spring.version}</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.springframework</groupId>
65+
<artifactId>spring-orm</artifactId>
66+
<version>${spring.version}</version>
67+
</dependency>
2468
</dependencies>
2569

2670
<build>
@@ -29,13 +73,87 @@
2973
<directory>src/test/resources</directory>
3074
<filtering>true</filtering>
3175
</resource>
76+
<resource>
77+
<directory>src/main/resources</directory>
78+
<filtering>true</filtering>
79+
</resource>
80+
3281
</resources>
3382
</build>
3483

84+
<profiles>
85+
<profile>
86+
<id>filtering</id>
87+
<activation>
88+
<activeByDefault>false</activeByDefault>
89+
</activation>
90+
<build>
91+
<plugins>
92+
<plugin>
93+
<artifactId>maven-surefire-plugin</artifactId>
94+
<version>${maven-surefire-plugin.version}</version>
95+
<dependencies>
96+
<dependency>
97+
<groupId>org.junit.platform</groupId>
98+
<artifactId>junit-platform-surefire-provider</artifactId>
99+
<version>${junit.platform.version}</version>
100+
</dependency>
101+
</dependencies>
102+
<configuration>
103+
<excludes>
104+
**/*IntegrationTest.java
105+
</excludes>
106+
</configuration>
107+
</plugin>
108+
</plugins>
109+
</build>
110+
</profile>
111+
<profile>
112+
<id>category</id>
113+
<activation>
114+
<activeByDefault>false</activeByDefault>
115+
</activation>
116+
<build>
117+
<plugins>
118+
<plugin>
119+
<artifactId>maven-surefire-plugin</artifactId>
120+
<version>${maven-surefire-plugin.version}</version>
121+
<configuration>
122+
<groups>com.baeldung.categories.UnitTest</groups>
123+
<excludedGroups>com.baeldung.categories.IntegrationTest</excludedGroups>
124+
</configuration>
125+
</plugin>
126+
</plugins>
127+
</build>
128+
</profile>
129+
<profile>
130+
<id>tags</id>
131+
<activation>
132+
<activeByDefault>false</activeByDefault>
133+
</activation>
134+
<build>
135+
<plugins>
136+
<plugin>
137+
<artifactId>maven-surefire-plugin</artifactId>
138+
<version>${maven-surefire-plugin.version}</version>
139+
<configuration>
140+
<groups>UnitTest</groups>
141+
<excludedGroups>IntegrationTest</excludedGroups>
142+
</configuration>
143+
</plugin>
144+
</plugins>
145+
</build>
146+
</profile>
147+
148+
</profiles>
149+
35150
<properties>
36151
<junit.jupiter.version>5.4.2</junit.jupiter.version>
37152
<junit.platform.version>1.2.0</junit.platform.version>
38153
<junit.vintage.version>5.2.0</junit.vintage.version>
154+
<h2.version>1.4.196</h2.version>
155+
<spring.version>5.0.6.RELEASE</spring.version>
156+
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
39157
</properties>
40158

41159
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.junit.tags.example;
2+
3+
public class Employee {
4+
private int id;
5+
6+
private String firstName;
7+
8+
private String lastName;
9+
10+
private String address;
11+
12+
public int getId() {
13+
return id;
14+
}
15+
16+
public void setId(final int id) {
17+
this.id = id;
18+
}
19+
20+
public String getFirstName() {
21+
return firstName;
22+
}
23+
24+
public void setFirstName(final String firstName) {
25+
this.firstName = firstName;
26+
}
27+
28+
public String getLastName() {
29+
return lastName;
30+
}
31+
32+
public void setLastName(final String lastName) {
33+
this.lastName = lastName;
34+
}
35+
36+
public String getAddress() {
37+
return address;
38+
}
39+
40+
public void setAddress(final String address) {
41+
this.address = address;
42+
}
43+
44+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.junit.tags.example;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.jdbc.core.JdbcTemplate;
5+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
6+
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
7+
import org.springframework.stereotype.Repository;
8+
9+
import javax.sql.DataSource;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
@Repository
15+
public class EmployeeDAO {
16+
17+
private JdbcTemplate jdbcTemplate;
18+
19+
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
20+
21+
private SimpleJdbcInsert simpleJdbcInsert;
22+
23+
@Autowired
24+
public void setDataSource(final DataSource dataSource) {
25+
jdbcTemplate = new JdbcTemplate(dataSource);
26+
27+
namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
28+
simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("EMPLOYEE");
29+
30+
}
31+
32+
public int getCountOfEmployees() {
33+
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class);
34+
}
35+
36+
public List<Employee> getAllEmployees() {
37+
return jdbcTemplate.query("SELECT * FROM EMPLOYEE", new EmployeeRowMapper());
38+
}
39+
40+
public int addEmplyee(final int id) {
41+
return jdbcTemplate.update("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?)", id, "Bill", "Gates", "USA");
42+
}
43+
44+
public int addEmplyeeUsingSimpelJdbcInsert(final Employee emp) {
45+
final Map<String, Object> parameters = new HashMap<String, Object>();
46+
parameters.put("ID", emp.getId());
47+
parameters.put("FIRST_NAME", emp.getFirstName());
48+
parameters.put("LAST_NAME", emp.getLastName());
49+
parameters.put("ADDRESS", emp.getAddress());
50+
51+
return simpleJdbcInsert.execute(parameters);
52+
}
53+
54+
// for testing
55+
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
56+
this.jdbcTemplate = jdbcTemplate;
57+
}
58+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.junit.tags.example;
2+
3+
import org.springframework.jdbc.core.RowMapper;
4+
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
8+
public class EmployeeRowMapper implements RowMapper<Employee> {
9+
10+
@Override
11+
public Employee mapRow(final ResultSet rs, final int rowNum) throws SQLException {
12+
final Employee employee = new Employee();
13+
14+
employee.setId(rs.getInt("ID"));
15+
employee.setFirstName(rs.getString("FIRST_NAME"));
16+
employee.setLastName(rs.getString("LAST_NAME"));
17+
employee.setAddress(rs.getString("ADDRESS"));
18+
19+
return employee;
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.junit.tags.example;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
7+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
8+
9+
import javax.sql.DataSource;
10+
11+
@Configuration
12+
@ComponentScan("com.baeldung.junit.tags.example")
13+
public class SpringJdbcConfig {
14+
15+
@Bean
16+
public DataSource dataSource() {
17+
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("classpath:jdbc/schema.sql").addScript("classpath:jdbc/test-data.sql").build();
18+
}
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE EMPLOYEE
2+
(
3+
ID int NOT NULL PRIMARY KEY,
4+
FIRST_NAME varchar(255),
5+
LAST_NAME varchar(255),
6+
ADDRESS varchar(255),
7+
);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
4+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
5+
>
6+
7+
<bean id="employeeDao" class="org.baeldung.jdbc.EmployeeDAO">
8+
<property name="dataSource" ref="dataSource"/>
9+
</bean>
10+
11+
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
12+
<property name="driverClassName" value="${jdbc.driverClassName}"/>
13+
<property name="url" value="${jdbc.url}"/>
14+
<property name="username" value="${jdbc.username}"/>
15+
<property name="password" value="${jdbc.password}"/>
16+
</bean>
17+
18+
<context:property-placeholder location="jdbc.properties"/>
19+
</beans>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
INSERT INTO EMPLOYEE VALUES (1, 'James', 'Gosling', 'Canada');
2+
3+
INSERT INTO EMPLOYEE VALUES (2, 'Donald', 'Knuth', 'USA');
4+
5+
INSERT INTO EMPLOYEE VALUES (3, 'Linus', 'Torvalds', 'Finland');
6+
7+
INSERT INTO EMPLOYEE VALUES (4, 'Dennis', 'Ritchie', 'USA');
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.categories;
2+
3+
import com.baeldung.junit.tags.example.Employee;
4+
import com.baeldung.junit.tags.example.EmployeeDAO;
5+
import com.baeldung.junit.tags.example.SpringJdbcConfig;
6+
import org.hamcrest.CoreMatchers;
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.junit.experimental.categories.Category;
11+
import org.junit.runner.RunWith;
12+
import org.mockito.Mock;
13+
import org.mockito.Mockito;
14+
import org.mockito.MockitoAnnotations;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.jdbc.core.JdbcTemplate;
17+
import org.springframework.test.context.ContextConfiguration;
18+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
19+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
20+
21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@ContextConfiguration(classes = { SpringJdbcConfig.class }, loader = AnnotationConfigContextLoader.class)
23+
public class EmployeeDAOCategoryIntegrationTest {
24+
25+
@Autowired
26+
private EmployeeDAO employeeDao;
27+
28+
@Mock
29+
private JdbcTemplate jdbcTemplate;
30+
private EmployeeDAO employeeDAO;
31+
32+
@Before
33+
public void setup() {
34+
MockitoAnnotations.initMocks(this);
35+
employeeDAO = new EmployeeDAO();
36+
employeeDAO.setJdbcTemplate(jdbcTemplate);
37+
}
38+
39+
@Test
40+
@Category(IntegrationTest.class)
41+
public void testAddEmployeeUsingSimpelJdbcInsert() {
42+
final Employee emp = new Employee();
43+
emp.setId(12);
44+
emp.setFirstName("testFirstName");
45+
emp.setLastName("testLastName");
46+
emp.setAddress("testAddress");
47+
48+
Assert.assertEquals(employeeDao.addEmplyeeUsingSimpelJdbcInsert(emp), 1);
49+
}
50+
51+
@Test
52+
@Category(UnitTest.class)
53+
public void givenNumberOfEmployeeWhenCountEmployeeThenCountMatch() {
54+
55+
// given
56+
Mockito.when(jdbcTemplate.queryForObject(Mockito.any(String.class), Mockito.eq(Integer.class)))
57+
.thenReturn(1);
58+
59+
// when
60+
int countOfEmployees = employeeDAO.getCountOfEmployees();
61+
62+
// then
63+
Assert.assertThat(countOfEmployees, CoreMatchers.is(1));
64+
}
65+
66+
}

0 commit comments

Comments
 (0)