Skip to content

Commit 3bb7ce5

Browse files
hpoettkerfmbenhassine
authored andcommitted
Improve @SpringBatchTest to autowire the job under test in JobLauncherTestUtils if it is unique
Issue #4218
1 parent 621ec92 commit 3bb7ce5

34 files changed

+201
-102
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.junit.jupiter.api.Test;
1919

20-
import org.springframework.batch.core.Job;
2120
import org.springframework.batch.core.explore.JobExplorer;
2221
import org.springframework.batch.test.JobLauncherTestUtils;
2322
import org.springframework.beans.factory.annotation.Autowired;
@@ -50,9 +49,8 @@ class AMQPJobFunctionalTests {
5049
private JobExplorer jobExplorer;
5150

5251
@Test
53-
void testLaunchJob(@Autowired Job job) throws Exception {
52+
void testLaunchJob() throws Exception {
5453
// given
55-
this.jobLauncherTestUtils.setJob(job);
5654
this.jobLauncherTestUtils.launchJob();
5755

5856
// when

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

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

21-
import org.springframework.batch.core.Job;
2221
import org.springframework.batch.test.JobLauncherTestUtils;
2322
import org.springframework.beans.factory.annotation.Autowired;
2423
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -31,10 +30,7 @@ class BeanWrapperMapperSampleJobFunctionalTests {
3130
private JobLauncherTestUtils jobLauncherTestUtils;
3231

3332
@Test
34-
void testJobLaunch(@Autowired Job job) throws Exception {
35-
// given
36-
this.jobLauncherTestUtils.setJob(job);
37-
33+
void testJobLaunch() throws Exception {
3834
// when
3935
this.jobLauncherTestUtils.launchJob();
4036

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.apache.commons.io.IOUtils;
2929
import org.junit.jupiter.api.Test;
3030

31-
import org.springframework.batch.core.Job;
3231
import org.springframework.batch.sample.domain.trade.Trade;
3332
import org.springframework.batch.test.JobLauncherTestUtils;
3433
import org.springframework.beans.factory.annotation.Autowired;
@@ -62,8 +61,7 @@ public void setDataSource(DataSource dataSource) {
6261
}
6362

6463
@Test
65-
void testJobLaunch(@Autowired Job job) throws Exception {
66-
this.jobLauncherTestUtils.setJob(job);
64+
void testJobLaunch() throws Exception {
6765
JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE");
6866
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE");
6967

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.junit.jupiter.api.BeforeEach;
3030
import org.junit.jupiter.api.Test;
3131

32-
import org.springframework.batch.core.Job;
3332
import org.springframework.batch.core.JobExecution;
3433
import org.springframework.batch.test.JobLauncherTestUtils;
3534
import org.springframework.beans.factory.annotation.Autowired;
@@ -82,8 +81,7 @@ void tearDown() {
8281
}
8382

8483
@Test
85-
void testFilterJob(@Autowired Job job) throws Exception {
86-
this.jobLauncherTestUtils.setJob(job);
84+
void testFilterJob() throws Exception {
8785
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
8886

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

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.batch.core.BatchStatus;
24-
import org.springframework.batch.core.Job;
2524
import org.springframework.batch.core.JobExecution;
2625
import org.springframework.batch.core.launch.JobOperator;
2726
import org.springframework.batch.test.JobLauncherTestUtils;
@@ -57,8 +56,7 @@ class DatabaseShutdownFunctionalTests {
5756
private JobLauncherTestUtils jobLauncherTestUtils;
5857

5958
@Test
60-
void testLaunchJob(@Autowired Job job) throws Exception {
61-
this.jobLauncherTestUtils.setJob(job);
59+
void testLaunchJob() throws Exception {
6260
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
6361

6462
Thread.sleep(1000);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.junit.jupiter.api.Test;
1919

20-
import org.springframework.batch.core.Job;
2120
import org.springframework.batch.sample.domain.person.PersonService;
2221
import org.springframework.batch.test.JobLauncherTestUtils;
2322
import org.springframework.beans.factory.annotation.Autowired;
@@ -37,8 +36,7 @@ class DelegatingJobFunctionalTests {
3736
private PersonService personService;
3837

3938
@Test
40-
void testLaunchJob(@Autowired Job job) throws Exception {
41-
this.jobLauncherTestUtils.setJob(job);
39+
void testLaunchJob() throws Exception {
4240
jobLauncherTestUtils.launchJob();
4341

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

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

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

22-
import org.springframework.batch.core.Job;
2322
import org.springframework.batch.test.JobLauncherTestUtils;
2423
import org.springframework.beans.factory.annotation.Autowired;
2524
import org.springframework.jdbc.core.JdbcTemplate;
@@ -43,8 +42,7 @@ public void setDataSource(DataSource dataSource) {
4342
}
4443

4544
@Test
46-
void testLaunchJob(@Autowired Job job) throws Exception {
47-
this.jobLauncherTestUtils.setJob(job);
45+
void testLaunchJob() throws Exception {
4846
JdbcTestUtils.deleteFromTables(jdbcTemplate, "PLAYERS", "GAMES", "PLAYER_SUMMARY");
4947

5048
jobLauncherTestUtils.launchJob();

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.batch.core.BatchStatus;
24-
import org.springframework.batch.core.Job;
2524
import org.springframework.batch.core.JobExecution;
2625
import org.springframework.batch.core.JobParameters;
2726
import org.springframework.batch.core.JobParametersBuilder;
@@ -58,8 +57,7 @@ class GracefulShutdownFunctionalTests {
5857
private JobOperator jobOperator;
5958

6059
@Test
61-
void testLaunchJob(@Autowired Job job) throws Exception {
62-
this.jobLauncherTestUtils.setJob(job);
60+
void testLaunchJob() throws Exception {
6361
final JobParameters jobParameters = new JobParametersBuilder().addLong("timestamp", System.currentTimeMillis())
6462
.toJobParameters();
6563

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.junit.jupiter.api.BeforeEach;
2424
import org.junit.jupiter.api.Test;
2525

26-
import org.springframework.batch.core.Job;
2726
import org.springframework.batch.test.JobLauncherTestUtils;
2827
import org.springframework.beans.factory.annotation.Autowired;
2928
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@@ -43,8 +42,7 @@ void removeOldData() throws IOException {
4342
}
4443

4544
@Test
46-
void testLaunchJob(@Autowired Job job) throws Exception {
47-
this.jobLauncherTestUtils.setJob(job);
45+
void testLaunchJob() throws Exception {
4846
assertFalse(new File("target/groovyJob/output/files.zip").exists());
4947
jobLauncherTestUtils.launchJob();
5048
assertTrue(new File("target/groovyJob/output/files.zip").exists());

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

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

23-
import org.springframework.batch.core.Job;
2423
import org.springframework.batch.test.JobLauncherTestUtils;
2524
import org.springframework.beans.factory.annotation.Autowired;
2625
import org.springframework.beans.factory.annotation.Qualifier;
@@ -45,8 +44,7 @@ class HeaderFooterSampleFunctionalTests {
4544
private JobLauncherTestUtils jobLauncherTestUtils;
4645

4746
@Test
48-
void testJob(@Autowired Job job) throws Exception {
49-
this.jobLauncherTestUtils.setJob(job);
47+
void testJob() throws Exception {
5048
this.jobLauncherTestUtils.launchJob();
5149

5250
BufferedReader inputReader = new BufferedReader(new FileReader(input.getFile()));

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import org.junit.jupiter.api.Test;
2727

28-
import org.springframework.batch.core.Job;
2928
import org.springframework.batch.core.JobParameters;
3029
import org.springframework.batch.core.JobParametersBuilder;
3130
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
@@ -92,8 +91,7 @@ public void setDataSource(DataSource dataSource) {
9291
}
9392

9493
@Test
95-
void testLaunchJob(@Autowired Job job) throws Exception {
96-
this.jobLauncherTestUtils.setJob(job);
94+
void testLaunchJob() throws Exception {
9795
validatePreConditions();
9896

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

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.junit.jupiter.api.Test;
1919

20-
import org.springframework.batch.core.Job;
2120
import org.springframework.batch.sample.domain.trade.internal.ItemTrackingTradeItemWriter;
2221
import org.springframework.batch.test.JobLauncherTestUtils;
2322
import org.springframework.beans.factory.annotation.Autowired;
@@ -45,8 +44,7 @@ class LoopFlowSampleFunctionalTests {
4544
private JobLauncherTestUtils jobLauncherTestUtils;
4645

4746
@Test
48-
void testJobLaunch(@Autowired Job job) throws Exception {
49-
this.jobLauncherTestUtils.setJob(job);
47+
void testJobLaunch() throws Exception {
5048
this.jobLauncherTestUtils.launchJob();
5149
// items processed = items read + 2 exceptions
5250
assertEquals(10, itemWriter.getItems().size());

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.junit.jupiter.api.Test;
2626

2727
import org.springframework.batch.core.ExitStatus;
28-
import org.springframework.batch.core.Job;
2928
import org.springframework.batch.core.JobExecution;
3029
import org.springframework.batch.sample.domain.mail.internal.TestMailErrorHandler;
3130
import org.springframework.batch.sample.domain.mail.internal.TestMailSender;
@@ -96,8 +95,7 @@ void after() {
9695
}
9796

9897
@Test
99-
void testSkip(@Autowired Job job) throws Exception {
100-
this.jobLauncherTestUtils.setJob(job);
98+
void testSkip() throws Exception {
10199
this.createUsers(new Object[][] { USER1, USER2_SKIP, USER3, USER4_SKIP, USER5, USER6, USER7, USER8 });
102100

103101
JobExecution jobExecution = jobLauncherTestUtils.launchJob();

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.apache.commons.io.IOUtils;
2020
import org.junit.jupiter.api.Test;
2121

22-
import org.springframework.batch.core.Job;
2322
import org.springframework.batch.test.JobLauncherTestUtils;
2423
import org.springframework.beans.factory.annotation.Autowired;
2524
import org.springframework.core.io.FileSystemResource;
@@ -44,8 +43,7 @@ class MultilineJobFunctionalTests {
4443
private final Resource output = new FileSystemResource("target/test-outputs/20070122.testStream.multilineStep.txt");
4544

4645
@Test
47-
void testJobLaunch(@Autowired Job job) throws Exception {
48-
this.jobLauncherTestUtils.setJob(job);
46+
void testJobLaunch() throws Exception {
4947
this.jobLauncherTestUtils.launchJob();
5048
assertEquals(EXPECTED_RESULT, StringUtils.replace(IOUtils.toString(output.getInputStream(), "UTF-8"),
5149
System.getProperty("line.separator"), ""));

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.junit.jupiter.api.Assertions;
2323
import org.junit.jupiter.api.Test;
2424

25-
import org.springframework.batch.core.Job;
2625
import org.springframework.batch.test.JobLauncherTestUtils;
2726
import org.springframework.beans.factory.annotation.Autowired;
2827
import org.springframework.core.io.ClassPathResource;
@@ -41,8 +40,7 @@ class MultilineOrderJobFunctionalTests {
4140
private JobLauncherTestUtils jobLauncherTestUtils;
4241

4342
@Test
44-
void testJobLaunch(@Autowired Job job) throws Exception {
45-
this.jobLauncherTestUtils.setJob(job);
43+
void testJobLaunch() throws Exception {
4644
this.jobLauncherTestUtils.launchJob();
4745
Path expectedFile = new ClassPathResource(EXPECTED).getFile().toPath();
4846
Path actualFile = new FileSystemResource(ACTUAL).getFile().toPath();

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.batch.core.BatchStatus;
24-
import org.springframework.batch.core.Job;
2524
import org.springframework.batch.core.JobExecution;
2625
import org.springframework.batch.test.JobLauncherTestUtils;
2726
import org.springframework.beans.factory.annotation.Autowired;
@@ -46,8 +45,7 @@ public void setDataSource(DataSource dataSource) {
4645
}
4746

4847
@Test
49-
void testLaunchJob(@Autowired Job job) throws Exception {
50-
this.jobLauncherTestUtils.setJob(job);
48+
void testLaunchJob() throws Exception {
5149
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING");
5250
JobExecution execution = jobLauncherTestUtils.launchJob();
5351
int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING");

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.junit.jupiter.api.Test;
2525

2626
import org.springframework.batch.core.BatchStatus;
27-
import org.springframework.batch.core.Job;
2827
import org.springframework.batch.core.JobExecution;
2928
import org.springframework.batch.item.ExecutionContext;
3029
import org.springframework.batch.item.ItemReader;
@@ -64,8 +63,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
6463
* Check the resulting credits correspond to inputs increased by fixed amount.
6564
*/
6665
@Test
67-
void testUpdateCredit(@Autowired Job job) throws Exception {
68-
this.jobLauncherTestUtils.setJob(job);
66+
void testUpdateCredit() throws Exception {
6967
assertTrue(applicationContext.containsBeanDefinition("outputTestReader"),
7068
"Define a prototype bean called 'outputTestReader' to check the output");
7169

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.junit.jupiter.api.Test;
2525

2626
import org.springframework.batch.core.BatchStatus;
27-
import org.springframework.batch.core.Job;
2827
import org.springframework.batch.core.JobExecution;
2928
import org.springframework.batch.item.ExecutionContext;
3029
import org.springframework.batch.item.ItemReader;
@@ -64,8 +63,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
6463
* Check the resulting credits correspond to inputs increased by fixed amount.
6564
*/
6665
@Test
67-
void testUpdateCredit(@Autowired Job job) throws Exception {
68-
this.jobLauncherTestUtils.setJob(job);
66+
void testUpdateCredit() throws Exception {
6967
assertTrue(applicationContext.containsBeanDefinition("outputTestReader"),
7068
"Define a prototype bean called 'outputTestReader' to check the output");
7169

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616

1717
package org.springframework.batch.sample;
1818

19-
import java.io.File;
2019
import java.nio.file.Files;
2120
import java.nio.file.Path;
2221

2322
import org.junit.jupiter.api.Assertions;
2423
import org.junit.jupiter.api.Test;
2524

2625
import org.springframework.batch.core.BatchStatus;
27-
import org.springframework.batch.core.Job;
2826
import org.springframework.batch.core.JobExecution;
2927
import org.springframework.batch.core.JobParameters;
3028
import org.springframework.batch.item.Chunk;
@@ -54,8 +52,7 @@ class RestartFileSampleFunctionalTests {
5452
private JobLauncherTestUtils jobLauncherTestUtils;
5553

5654
@Test
57-
void runTest(@Autowired Job job) throws Exception {
58-
this.jobLauncherTestUtils.setJob(job);
55+
void runTest() throws Exception {
5956
JobParameters jobParameters = jobLauncherTestUtils.getUniqueJobParameters();
6057

6158
JobExecution je1 = jobLauncherTestUtils.launchJob(jobParameters);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.batch.core.BatchStatus;
24-
import org.springframework.batch.core.Job;
2524
import org.springframework.batch.core.JobExecution;
2625
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
2726
import org.springframework.batch.support.PropertiesConverter;
@@ -70,8 +69,7 @@ void onTearDown() {
7069
* @throws Exception
7170
*/
7271
@Test
73-
void testLaunchJob(@Autowired Job job) throws Exception {
74-
this.jobLauncherTestUtils.setJob(job);
72+
void testLaunchJob() throws Exception {
7573
int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE");
7674

7775
JobExecution jobExecution = runJobForRestartTest();

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.junit.jupiter.api.Test;
1919

20-
import org.springframework.batch.core.Job;
2120
import org.springframework.batch.sample.domain.trade.internal.GeneratingTradeItemReader;
2221
import org.springframework.batch.sample.support.RetrySampleItemWriter;
2322
import org.springframework.batch.test.JobLauncherTestUtils;
@@ -48,8 +47,7 @@ class RetrySampleFunctionalTests {
4847
private JobLauncherTestUtils jobLauncherTestUtils;
4948

5049
@Test
51-
void testLaunchJob(@Autowired Job job) throws Exception {
52-
this.jobLauncherTestUtils.setJob(job);
50+
void testLaunchJob() throws Exception {
5351
this.jobLauncherTestUtils.launchJob();
5452
// items processed = items read + 2 exceptions
5553
assertEquals(itemGenerator.getLimit() + 2, itemProcessor.getCounter());

0 commit comments

Comments
 (0)