Skip to content

Commit d528111

Browse files
committed
Updated the Spring Samples Code to JUnit 5.
Since I had to add the dependency to the tests to have them pass because of the change made in the previous commit, I decided to migrate all tests to JUnit 5. 2 things to note: * I used Spring's AssertionErrors on some AssertTrue or AssertFalse methods so that we could maintain the message. * In a couple of cases the use of assertNotSame that was used had a message, but is no longer supported, so I removed those messages.
1 parent 984d3b3 commit d528111

File tree

84 files changed

+838
-577
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+838
-577
lines changed

.vscode/launch.json

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "java",
9+
"name": "Launch Current File",
10+
"request": "launch",
11+
"mainClass": "${file}"
12+
},
13+
{
14+
"type": "java",
15+
"name": "Launch CommandLineJobRunner",
16+
"request": "launch",
17+
"mainClass": "org.springframework.batch.core.launch.support.CommandLineJobRunner",
18+
"projectName": "spring-batch-core"
19+
},
20+
{
21+
"type": "java",
22+
"name": "Launch JobRegistryBackgroundJobRunner",
23+
"request": "launch",
24+
"mainClass": "org.springframework.batch.core.launch.support.JobRegistryBackgroundJobRunner",
25+
"projectName": "spring-batch-core"
26+
},
27+
{
28+
"type": "java",
29+
"name": "Launch DataSourceInitializer",
30+
"request": "launch",
31+
"mainClass": "test.jdbc.datasource.DataSourceInitializer",
32+
"projectName": "spring-batch-core"
33+
},
34+
{
35+
"type": "java",
36+
"name": "Launch AvroTestUtils",
37+
"request": "launch",
38+
"mainClass": "org.springframework.batch.item.avro.example.AvroTestUtils",
39+
"projectName": "spring-batch-infrastructure"
40+
},
41+
{
42+
"type": "java",
43+
"name": "Launch DataSourceInitializer(1)",
44+
"request": "launch",
45+
"mainClass": "test.jdbc.datasource.DataSourceInitializer",
46+
"projectName": "spring-batch-infrastructure"
47+
},
48+
{
49+
"type": "java",
50+
"name": "Launch BatchMetricsApplication",
51+
"request": "launch",
52+
"mainClass": "org.springframework.batch.sample.metrics.BatchMetricsApplication",
53+
"projectName": "spring-batch-samples"
54+
},
55+
{
56+
"type": "java",
57+
"name": "Launch MongoDBSampleApp",
58+
"request": "launch",
59+
"mainClass": "org.springframework.batch.sample.mongodb.MongoDBSampleApp",
60+
"projectName": "spring-batch-samples"
61+
},
62+
{
63+
"type": "java",
64+
"name": "Launch AmqpMessageProducer",
65+
"request": "launch",
66+
"mainClass": "org.springframework.batch.sample.rabbitmq.amqp.AmqpMessageProducer",
67+
"projectName": "spring-batch-samples"
68+
},
69+
{
70+
"type": "java",
71+
"name": "Launch DataSourceInitializer(2)",
72+
"request": "launch",
73+
"mainClass": "org.springframework.batch.test.DataSourceInitializer",
74+
"projectName": "spring-batch-test"
75+
},
76+
{
77+
"type": "java",
78+
"name": "Launch Task Sample app",
79+
"request": "launch",
80+
"mainClass": "org.springframework.batch.test.DataSourceInitializer",
81+
"projectName": "spring-batch-sample-apps"
82+
}
83+
]
84+
}

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "automatic"
3+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "java",
9+
"name": "Launch Current File",
10+
"request": "launch",
11+
"mainClass": "${file}"
12+
},
13+
{
14+
"type": "java",
15+
"name": "Launch TaskletSampleApp",
16+
"request": "launch",
17+
"mainClass": "io.spring.taskletsampleapp.TaskletSampleApp",
18+
"projectName": "tasklet-sample-app"
19+
},
20+
{
21+
"type": "java",
22+
"name": "Launch TaskletSampleApp For XML",
23+
"request": "launch",
24+
"mainClass": "io.spring.taskletsampleapp.TaskletSampleApp",
25+
"projectName": "tasklet-sample-app",
26+
"vmArgs": "-Dspring.profiles.active=xml -Dspring.batch.initializer.enabled=true"
27+
}
28+
]
29+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "automatic"
3+
}

spring-batch-samples/pom.xml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,6 @@
158158
<artifactId>spring-batch-test</artifactId>
159159
<scope>test</scope>
160160
</dependency>
161-
<dependency>
162-
<groupId>junit</groupId>
163-
<artifactId>junit</artifactId>
164-
<version>${junit.version}</version>
165-
<scope>test</scope>
166-
<exclusions>
167-
<exclusion>
168-
<groupId>org.hamcrest</groupId>
169-
<artifactId>hamcrest-core</artifactId>
170-
</exclusion>
171-
</exclusions>
172-
</dependency>
173161
<dependency>
174162
<groupId>org.junit.jupiter</groupId>
175163
<artifactId>junit-jupiter-engine</artifactId>
@@ -179,7 +167,7 @@
179167
<groupId>org.junit.jupiter</groupId>
180168
<artifactId>junit-jupiter-api</artifactId>
181169
<version>${junit-jupiter.version}</version>
182-
<optional>true</optional>
170+
<scope>test</scope>
183171
</dependency>
184172
<dependency>
185173
<groupId>org.hamcrest</groupId>

spring-batch-samples/src/test/java/org/springframework/batch/sample/AMQPJobFunctionalTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919

2020
import org.junit.jupiter.api.Test;
2121

22+
import org.junit.jupiter.api.extension.ExtendWith;
2223
import org.springframework.batch.core.Job;
2324
import org.springframework.batch.core.explore.JobExplorer;
2425
import org.springframework.batch.test.JobLauncherTestUtils;
2526
import org.springframework.beans.factory.annotation.Autowired;
26-
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
27+
import org.springframework.test.context.ContextConfiguration;
28+
import org.springframework.test.context.junit.jupiter.SpringExtension;
2729

2830
/**
2931
* <p>
@@ -39,9 +41,10 @@
3941
* </p>
4042
*/
4143

42-
@SpringJUnitConfig(
44+
@ExtendWith(SpringExtension.class)
45+
@ContextConfiguration(
4346
locations = { "/simple-job-launcher-context.xml", "/jobs/amqp-example-job.xml", "/job-runner-context.xml" })
44-
class AMQPJobFunctionalTests {
47+
public class AMQPJobFunctionalTests {
4548

4649
@Autowired
4750
private JobLauncherTestUtils jobLauncherTestUtils;

spring-batch-samples/src/test/java/org/springframework/batch/sample/BeanWrapperMapperSampleJobFunctionalTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package org.springframework.batch.sample;
1818

1919
import org.junit.jupiter.api.Test;
20-
20+
import org.junit.jupiter.api.extension.ExtendWith;
2121
import org.springframework.batch.core.Job;
2222
import org.springframework.batch.test.JobLauncherTestUtils;
2323
import org.springframework.beans.factory.annotation.Autowired;
24-
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
24+
import org.springframework.test.context.ContextConfiguration;
25+
import org.springframework.test.context.junit.jupiter.SpringExtension;
2526

26-
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/jobs/beanWrapperMapperSampleJob.xml",
27+
@ExtendWith(SpringExtension.class)
28+
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/beanWrapperMapperSampleJob.xml",
2729
"/job-runner-context.xml" })
2830
class BeanWrapperMapperSampleJobFunctionalTests {
2931

spring-batch-samples/src/test/java/org/springframework/batch/sample/CompositeItemWriterSampleFunctionalTests.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,34 @@
1515
*/
1616
package org.springframework.batch.sample;
1717

18-
import java.io.FileInputStream;
19-
import java.io.IOException;
20-
import java.math.BigDecimal;
21-
import java.sql.ResultSet;
22-
import java.sql.SQLException;
23-
import java.util.ArrayList;
24-
import java.util.List;
25-
import javax.sql.DataSource;
26-
2718
import org.apache.commons.io.IOUtils;
2819
import org.junit.jupiter.api.Test;
29-
20+
import org.junit.jupiter.api.extension.ExtendWith;
3021
import org.springframework.batch.core.Job;
3122
import org.springframework.batch.sample.domain.trade.Trade;
3223
import org.springframework.batch.test.JobLauncherTestUtils;
3324
import org.springframework.beans.factory.annotation.Autowired;
3425
import org.springframework.jdbc.core.JdbcTemplate;
3526
import org.springframework.jdbc.core.RowCallbackHandler;
36-
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
27+
import org.springframework.test.context.ContextConfiguration;
28+
import org.springframework.test.context.junit.jupiter.SpringExtension;
3729
import org.springframework.test.jdbc.JdbcTestUtils;
3830

31+
import javax.sql.DataSource;
32+
import java.io.FileInputStream;
33+
import java.io.IOException;
34+
import java.math.BigDecimal;
35+
import java.sql.ResultSet;
36+
import java.sql.SQLException;
37+
import java.util.ArrayList;
38+
import java.util.List;
39+
3940
import static org.junit.jupiter.api.Assertions.assertEquals;
4041

41-
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/jobs/compositeItemWriterSampleJob.xml",
42+
@ExtendWith(SpringExtension.class)
43+
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/compositeItemWriterSampleJob.xml",
4244
"/job-runner-context.xml" })
43-
class CompositeItemWriterSampleFunctionalTests {
45+
public class CompositeItemWriterSampleFunctionalTests {
4446

4547
private static final String GET_TRADES = "SELECT isin, quantity, price, customer FROM TRADE order by isin";
4648

spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,34 @@
1616

1717
package org.springframework.batch.sample;
1818

19-
import static org.junit.jupiter.api.Assertions.assertEquals;
20-
21-
import java.sql.ResultSet;
22-
import java.sql.SQLException;
23-
import java.util.Arrays;
24-
import java.util.HashMap;
25-
import java.util.List;
26-
import java.util.Map;
27-
28-
import javax.sql.DataSource;
29-
3019
import org.junit.jupiter.api.AfterEach;
3120
import org.junit.jupiter.api.BeforeEach;
3221
import org.junit.jupiter.api.Test;
33-
22+
import org.junit.jupiter.api.extension.ExtendWith;
3423
import org.springframework.batch.core.Job;
3524
import org.springframework.batch.core.JobExecution;
3625
import org.springframework.batch.test.JobLauncherTestUtils;
3726
import org.springframework.beans.factory.annotation.Autowired;
3827
import org.springframework.jdbc.core.JdbcTemplate;
3928
import org.springframework.jdbc.core.RowCallbackHandler;
40-
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
29+
import org.springframework.test.context.ContextConfiguration;
30+
import org.springframework.test.context.junit.jupiter.SpringExtension;
4131
import org.springframework.test.jdbc.JdbcTestUtils;
4232

43-
@SpringJUnitConfig(
33+
import javax.sql.DataSource;
34+
import java.sql.ResultSet;
35+
import java.sql.SQLException;
36+
import java.util.Arrays;
37+
import java.util.HashMap;
38+
import java.util.List;
39+
import java.util.Map;
40+
41+
import static org.junit.jupiter.api.Assertions.assertEquals;
42+
43+
@ExtendWith(SpringExtension.class)
44+
@ContextConfiguration(
4445
locations = { "/simple-job-launcher-context.xml", "/jobs/customerFilterJob.xml", "/job-runner-context.xml" })
45-
class CustomerFilterJobFunctionalTests {
46+
public class CustomerFilterJobFunctionalTests {
4647

4748
private static final String GET_CUSTOMERS = "select NAME, CREDIT from CUSTOMER order by NAME";
4849

@@ -63,7 +64,8 @@ public void setDataSource(DataSource dataSource) {
6364
}
6465

6566
@BeforeEach
66-
void onSetUp() {
67+
68+
public void onSetUp() throws Exception {
6769
JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE");
6870
JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "CUSTOMER", "ID > 4");
6971
jdbcTemplate.update("update CUSTOMER set credit=100000");
@@ -76,7 +78,7 @@ void onSetUp() {
7678
}
7779

7880
@AfterEach
79-
void tearDown() {
81+
public void tearDown() throws Exception {
8082
JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE");
8183
JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "CUSTOMER", "ID > 4");
8284
}
@@ -95,7 +97,7 @@ void testFilterJob(@Autowired Job job) throws Exception {
9597
jdbcTemplate.query(GET_CUSTOMERS, new RowCallbackHandler() {
9698
@Override
9799
public void processRow(ResultSet rs) throws SQLException {
98-
Customer customer = customers.get(activeRow++);
100+
CustomerFilterJobFunctionalTests.Customer customer = customers.get(activeRow++);
99101
assertEquals(customer.getName(), rs.getString(1));
100102
assertEquals(customer.getCredit(), rs.getDouble(2), .01);
101103
}

spring-batch-samples/src/test/java/org/springframework/batch/sample/DatabaseShutdownFunctionalTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,40 @@
2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
2626
import org.junit.jupiter.api.Test;
27+
import org.apache.commons.logging.Log;
28+
import org.apache.commons.logging.LogFactory;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.extension.ExtendWith;
31+
2732
import org.springframework.batch.core.BatchStatus;
2833
import org.springframework.batch.core.Job;
2934
import org.springframework.batch.core.JobExecution;
3035
import org.springframework.batch.core.launch.JobOperator;
3136
import org.springframework.batch.test.JobLauncherTestUtils;
3237
import org.springframework.beans.factory.annotation.Autowired;
3338
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
39+
import org.springframework.test.context.ContextConfiguration;
40+
import org.springframework.test.context.junit.jupiter.SpringExtension;
41+
42+
import static org.junit.jupiter.api.Assertions.assertEquals;
43+
import static org.junit.jupiter.api.Assertions.assertNotNull;
44+
import static org.junit.jupiter.api.Assertions.assertTrue;
45+
import static org.springframework.test.util.AssertionErrors.assertFalse;
3446

3547
/**
3648
* Functional test for graceful shutdown. A batch container is started in a new thread,
3749
* then it's stopped using {@link JobOperator#stop(long)}}.
3850
*
3951
* @author Lucas Ward
4052
* @author Mahmoud Ben Hassine
53+
* @author Glenn Renfro
4154
*
4255
*/
43-
@SpringJUnitConfig(
56+
57+
@ExtendWith(SpringExtension.class)
58+
@ContextConfiguration(
4459
locations = { "/simple-job-launcher-context.xml", "/jobs/infiniteLoopJob.xml", "/job-runner-context.xml" })
45-
class DatabaseShutdownFunctionalTests {
60+
public class DatabaseShutdownFunctionalTests {
4661

4762
/** Logger */
4863
protected final Log logger = LogFactory.getLog(getClass());

0 commit comments

Comments
 (0)