Skip to content

Commit c506a49

Browse files
committed
Remove job autowiring in JobLauncherTestUtils
Before this commit, registering the JobLauncherTestUtils as a bean in the test context (either manually or via `@SpringBatchTest`) was failing when multiple jobs are defined in the test context. This commit removes the autowiring of the job under test in JobLauncherTestUtils. Resolves #1237
1 parent e67c006 commit c506a49

File tree

37 files changed

+195
-86
lines changed

37 files changed

+195
-86
lines changed

spring-batch-docs/src/main/asciidoc/testing.adoc

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,15 @@ approach.
2222
For the unit test to run a batch job, the framework must load the job's
2323
`ApplicationContext`. Two annotations are used to trigger this behavior:
2424

25-
* `@RunWith(SpringJUnit4ClassRunner.class)` indicates that the class should use Spring's
25+
* `@SpringJUnitConfig` indicates that the class should use Spring's
2626
JUnit facilities
27-
* `@ContextConfiguration(...)` indicates which resources to configure the
28-
`ApplicationContext` with.
27+
* `@SpringBatchTest` injects Spring Batch test utilities (such as the
28+
`JobLauncherTestUtils` and `JobRepositoryTestUtils`) in the test context
2929

30-
Starting from v4.1, it is also possible to inject Spring Batch test utilities
31-
(such as the `JobLauncherTestUtils` and `JobRepositoryTestUtils`) in the test context
32-
by using the `@SpringBatchTest` annotation.
33-
34-
NOTE: Note that `JobLauncherTestUtils` requires a `Job` bean and that
35-
`JobRepositoryTestUtils` requires a `DataSource` bean. Since `@SpringBatchTest`
36-
registers a `JobLauncherTestUtils` and a `JobRepositoryTestUtils` in the test
30+
NOTE: Note that `JobRepositoryTestUtils` requires a `DataSource` bean. Since
31+
`@SpringBatchTest` registers a `JobRepositoryTestUtils` in the test
3732
context, it is expected that the test context contains a single autowire candidate
38-
for a `Job` and a `DataSource` (either a single bean definition or one that is
33+
for a `DataSource` (either a single bean definition or one that is
3934
annotated with `org.springframework.context.annotation.Primary`).
4035

4136
[role="javaContent"]
@@ -45,8 +40,7 @@ The following Java example shows the annotations in use:
4540
[source, java, role="javaContent"]
4641
----
4742
@SpringBatchTest
48-
@RunWith(SpringRunner.class)
49-
@ContextConfiguration(classes=SkipSampleConfiguration.class)
43+
@SpringJUnitConfig(SkipSampleConfiguration.class)
5044
public class SkipSampleFunctionalTests { ... }
5145
----
5246

@@ -57,8 +51,7 @@ The following XML example shows the annotations in use:
5751
[source, java, role="xmlContent"]
5852
----
5953
@SpringBatchTest
60-
@RunWith(SpringRunner.class)
61-
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml",
54+
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml",
6255
"/jobs/skipSampleJob.xml" })
6356
public class SkipSampleFunctionalTests { ... }
6457
----
@@ -81,14 +74,13 @@ about the `Job` run. In the following case, the test verifies that the `Job` end
8174
a status of `COMPLETED`.
8275

8376
[role="xmlContent"]
84-
The following listing shows the example in XML:
77+
The following listing shows an example with JUnit 5 in XML configuration style:
8578

8679
.XML Based Configuration
8780
[source, java, role="xmlContent"]
8881
----
8982
@SpringBatchTest
90-
@RunWith(SpringRunner.class)
91-
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml",
83+
@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml",
9284
"/jobs/skipSampleJob.xml" })
9385
public class SkipSampleFunctionalTests {
9486
@@ -103,7 +95,8 @@ public class SkipSampleFunctionalTests {
10395
}
10496
10597
@Test
106-
public void testJob() throws Exception {
98+
public void testJob(@Autowired Job job) throws Exception {
99+
this.jobLauncherTestUtils.setJob(job);
107100
simpleJdbcTemplate.update("delete from CUSTOMER");
108101
for (int i = 1; i <= 10; i++) {
109102
simpleJdbcTemplate.update("insert into CUSTOMER values (?, 0, ?, 100000)",
@@ -119,14 +112,13 @@ public class SkipSampleFunctionalTests {
119112
----
120113

121114
[role="javaContent"]
122-
The following listing shows the example in Java:
115+
The following listing shows an example with JUnit 5 in Java configuration style:
123116

124117
.Java Based Configuration
125118
[source, java, role="javaContent"]
126119
----
127120
@SpringBatchTest
128-
@RunWith(SpringRunner.class)
129-
@ContextConfiguration(classes=SkipSampleConfiguration.class)
121+
@SpringJUnitConfig(SkipSampleConfiguration.class)
130122
public class SkipSampleFunctionalTests {
131123
132124
@Autowired
@@ -140,7 +132,8 @@ public class SkipSampleFunctionalTests {
140132
}
141133
142134
@Test
143-
public void testJob() throws Exception {
135+
public void testJob(@Autowired Job job) throws Exception {
136+
this.jobLauncherTestUtils.setJob(job);
144137
simpleJdbcTemplate.update("delete from CUSTOMER");
145138
for (int i = 1; i <= 10; i++) {
146139
simpleJdbcTemplate.update("insert into CUSTOMER values (?, 0, ?, 100000)",
@@ -186,10 +179,9 @@ context for each test method, as the following example shows:
186179

187180
[source, java]
188181
----
189-
@ContextConfiguration
182+
@SpringJUnitConfig
190183
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
191184
StepScopeTestExecutionListener.class })
192-
@RunWith(SpringRunner.class)
193185
public class StepScopeTestExecutionListenerIntegrationTests {
194186
195187
// This component is defined step-scoped, so it cannot be injected unless
@@ -228,8 +220,7 @@ example can be configured as follows:
228220
[source, java]
229221
----
230222
@SpringBatchTest
231-
@RunWith(SpringRunner.class)
232-
@ContextConfiguration
223+
@SpringJUnitConfig
233224
public class StepScopeTestExecutionListenerIntegrationTests {
234225
235226
// This component is defined step-scoped, so it cannot be injected unless

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import static org.junit.jupiter.api.Assertions.assertTrue;
1919

2020
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.batch.core.Job;
2123
import org.springframework.batch.core.explore.JobExplorer;
2224
import org.springframework.batch.test.JobLauncherTestUtils;
2325
import org.springframework.beans.factory.annotation.Autowired;
@@ -48,12 +50,15 @@ class AMQPJobFunctionalTests {
4850
private JobExplorer jobExplorer;
4951

5052
@Test
51-
void testLaunchJob() throws Exception {
52-
53-
jobLauncherTestUtils.launchJob();
53+
void testLaunchJob(@Autowired Job job) throws Exception {
54+
// given
55+
this.jobLauncherTestUtils.setJob(job);
56+
this.jobLauncherTestUtils.launchJob();
5457

58+
// when
5559
int count = jobExplorer.getJobInstances("amqp-example-job", 0, 1).size();
5660

61+
// then
5762
assertTrue(count > 0);
5863

5964
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.batch.sample;
1818

1919
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.batch.core.Job;
2022
import org.springframework.batch.test.JobLauncherTestUtils;
2123
import org.springframework.beans.factory.annotation.Autowired;
2224
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -29,10 +31,15 @@ class BeanWrapperMapperSampleJobFunctionalTests {
2931
private JobLauncherTestUtils jobLauncherTestUtils;
3032

3133
@Test
32-
void testJobLaunch() throws Exception {
34+
void testJobLaunch(@Autowired Job job) throws Exception {
35+
// given
36+
this.jobLauncherTestUtils.setJob(job);
3337

34-
jobLauncherTestUtils.launchJob();
38+
// when
39+
this.jobLauncherTestUtils.launchJob();
3540

41+
// then
42+
// FIXME no assertions?
3643
}
3744

3845
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.commons.io.IOUtils;
2828
import org.junit.jupiter.api.Test;
2929

30+
import org.springframework.batch.core.Job;
3031
import org.springframework.batch.sample.domain.trade.Trade;
3132
import org.springframework.batch.test.JobLauncherTestUtils;
3233
import org.springframework.beans.factory.annotation.Autowired;
@@ -60,7 +61,8 @@ public void setDataSource(DataSource dataSource) {
6061
}
6162

6263
@Test
63-
void testJobLaunch() throws Exception {
64+
void testJobLaunch(@Autowired Job job) throws Exception {
65+
this.jobLauncherTestUtils.setJob(job);
6466
JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE");
6567
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE");
6668

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.junit.jupiter.api.AfterEach;
3131
import org.junit.jupiter.api.BeforeEach;
3232
import org.junit.jupiter.api.Test;
33+
34+
import org.springframework.batch.core.Job;
3335
import org.springframework.batch.core.JobExecution;
3436
import org.springframework.batch.test.JobLauncherTestUtils;
3537
import org.springframework.beans.factory.annotation.Autowired;
@@ -80,7 +82,8 @@ void tearDown() {
8082
}
8183

8284
@Test
83-
void testFilterJob() throws Exception {
85+
void testFilterJob(@Autowired Job job) throws Exception {
86+
this.jobLauncherTestUtils.setJob(job);
8487
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
8588

8689
customers = Arrays.asList(new Customer("customer1", (credits.get("customer1"))),

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.commons.logging.LogFactory;
2626
import org.junit.jupiter.api.Test;
2727
import org.springframework.batch.core.BatchStatus;
28+
import org.springframework.batch.core.Job;
2829
import org.springframework.batch.core.JobExecution;
2930
import org.springframework.batch.core.launch.JobOperator;
3031
import org.springframework.batch.test.JobLauncherTestUtils;
@@ -53,8 +54,8 @@ class DatabaseShutdownFunctionalTests {
5354
private JobLauncherTestUtils jobLauncherTestUtils;
5455

5556
@Test
56-
void testLaunchJob() throws Exception {
57-
57+
void testLaunchJob(@Autowired Job job) throws Exception {
58+
this.jobLauncherTestUtils.setJob(job);
5859
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
5960

6061
Thread.sleep(1000);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.junit.jupiter.api.Assertions.assertTrue;
2020

2121
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.batch.core.Job;
2224
import org.springframework.batch.sample.domain.person.PersonService;
2325
import org.springframework.batch.test.JobLauncherTestUtils;
2426
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,8 +37,8 @@ class DelegatingJobFunctionalTests {
3537
private PersonService personService;
3638

3739
@Test
38-
void testLaunchJob() throws Exception {
39-
40+
void testLaunchJob(@Autowired Job job) throws Exception {
41+
this.jobLauncherTestUtils.setJob(job);
4042
jobLauncherTestUtils.launchJob();
4143

4244
assertTrue(personService.getReturnedCount() > 0);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import javax.sql.DataSource;
2121

2222
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.batch.core.Job;
2325
import org.springframework.batch.test.JobLauncherTestUtils;
2426
import org.springframework.beans.factory.annotation.Autowired;
2527
import org.springframework.jdbc.core.JdbcTemplate;
@@ -41,7 +43,8 @@ public void setDataSource(DataSource dataSource) {
4143
}
4244

4345
@Test
44-
void testLaunchJob() throws Exception {
46+
void testLaunchJob(@Autowired Job job) throws Exception {
47+
this.jobLauncherTestUtils.setJob(job);
4548
JdbcTestUtils.deleteFromTables(jdbcTemplate, "PLAYERS", "GAMES", "PLAYER_SUMMARY");
4649

4750
jobLauncherTestUtils.launchJob();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.commons.logging.LogFactory;
2525
import org.junit.jupiter.api.Test;
2626
import org.springframework.batch.core.BatchStatus;
27+
import org.springframework.batch.core.Job;
2728
import org.springframework.batch.core.JobExecution;
2829
import org.springframework.batch.core.JobParameters;
2930
import org.springframework.batch.core.JobParametersBuilder;
@@ -54,8 +55,8 @@ class GracefulShutdownFunctionalTests {
5455
private JobOperator jobOperator;
5556

5657
@Test
57-
void testLaunchJob() throws Exception {
58-
58+
void testLaunchJob(@Autowired Job job) throws Exception {
59+
this.jobLauncherTestUtils.setJob(job);
5960
final JobParameters jobParameters = new JobParametersBuilder().addLong("timestamp", System.currentTimeMillis())
6061
.toJobParameters();
6162

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.apache.commons.io.FileUtils;
2626
import org.junit.jupiter.api.BeforeEach;
2727
import org.junit.jupiter.api.Test;
28+
29+
import org.springframework.batch.core.Job;
2830
import org.springframework.batch.test.JobLauncherTestUtils;
2931
import org.springframework.beans.factory.annotation.Autowired;
3032
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -41,7 +43,8 @@ void removeOldData() throws IOException {
4143
}
4244

4345
@Test
44-
void testLaunchJob() throws Exception {
46+
void testLaunchJob(@Autowired Job job) throws Exception {
47+
this.jobLauncherTestUtils.setJob(job);
4548
assertFalse(new File("target/groovyJob/output/files.zip").exists());
4649
jobLauncherTestUtils.launchJob();
4750
assertTrue(new File("target/groovyJob/output/files.zip").exists());

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.FileReader;
2222

2323
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.batch.core.Job;
2426
import org.springframework.batch.test.JobLauncherTestUtils;
2527
import org.springframework.beans.factory.annotation.Autowired;
2628
import org.springframework.beans.factory.annotation.Qualifier;
@@ -43,8 +45,9 @@ class HeaderFooterSampleFunctionalTests {
4345
private JobLauncherTestUtils jobLauncherTestUtils;
4446

4547
@Test
46-
void testJob() throws Exception {
47-
jobLauncherTestUtils.launchJob();
48+
void testJob(@Autowired Job job) throws Exception {
49+
this.jobLauncherTestUtils.setJob(job);
50+
this.jobLauncherTestUtils.launchJob();
4851

4952
BufferedReader inputReader = new BufferedReader(new FileReader(input.getFile()));
5053
BufferedReader outputReader = new BufferedReader(new FileReader(output.getFile()));

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.junit.jupiter.api.Test;
3030

31+
import org.springframework.batch.core.Job;
3132
import org.springframework.batch.core.JobParameters;
3233
import org.springframework.batch.core.JobParametersBuilder;
3334
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
@@ -89,7 +90,8 @@ public void setDataSource(DataSource dataSource) {
8990
}
9091

9192
@Test
92-
void testLaunchJob() throws Exception {
93+
void testLaunchJob(@Autowired Job job) throws Exception {
94+
this.jobLauncherTestUtils.setJob(job);
9395
validatePreConditions();
9496

9597
JobParameters params = new JobParametersBuilder().addString("key", "failureJob").toJobParameters();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import static org.junit.jupiter.api.Assertions.assertEquals;
1919

2020
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.batch.core.Job;
2123
import org.springframework.batch.sample.domain.trade.internal.ItemTrackingTradeItemWriter;
2224
import org.springframework.batch.test.JobLauncherTestUtils;
2325
import org.springframework.beans.factory.annotation.Autowired;
@@ -40,8 +42,9 @@ class LoopFlowSampleFunctionalTests {
4042
private JobLauncherTestUtils jobLauncherTestUtils;
4143

4244
@Test
43-
void testJobLaunch() throws Exception {
44-
jobLauncherTestUtils.launchJob();
45+
void testJobLaunch(@Autowired Job job) throws Exception {
46+
this.jobLauncherTestUtils.setJob(job);
47+
this.jobLauncherTestUtils.launchJob();
4548
// items processed = items read + 2 exceptions
4649
assertEquals(10, itemWriter.getItems().size());
4750
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.jupiter.api.BeforeEach;
2727
import org.junit.jupiter.api.Test;
2828
import org.springframework.batch.core.ExitStatus;
29+
import org.springframework.batch.core.Job;
2930
import org.springframework.batch.core.JobExecution;
3031
import org.springframework.batch.sample.domain.mail.internal.TestMailErrorHandler;
3132
import org.springframework.batch.sample.domain.mail.internal.TestMailSender;
@@ -93,7 +94,8 @@ void after() {
9394
}
9495

9596
@Test
96-
void testSkip() throws Exception {
97+
void testSkip(@Autowired Job job) throws Exception {
98+
this.jobLauncherTestUtils.setJob(job);
9799
this.createUsers(new Object[][] { USER1, USER2_SKIP, USER3, USER4_SKIP, USER5, USER6, USER7, USER8 });
98100

99101
JobExecution jobExecution = jobLauncherTestUtils.launchJob();

0 commit comments

Comments
 (0)