diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/AMQPJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/AMQPJobFunctionalTests.java index 8cd6e89555..c283a9ab2c 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/AMQPJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/AMQPJobFunctionalTests.java @@ -17,7 +17,6 @@ import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.core.explore.JobExplorer; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -50,9 +49,8 @@ class AMQPJobFunctionalTests { private JobExplorer jobExplorer; @Test - void testLaunchJob(@Autowired Job job) throws Exception { + void testLaunchJob() throws Exception { // given - this.jobLauncherTestUtils.setJob(job); this.jobLauncherTestUtils.launchJob(); // when diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/BeanWrapperMapperSampleJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/BeanWrapperMapperSampleJobFunctionalTests.java index 8595d981d3..ab338a5899 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/BeanWrapperMapperSampleJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/BeanWrapperMapperSampleJobFunctionalTests.java @@ -18,7 +18,6 @@ import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @@ -31,10 +30,7 @@ class BeanWrapperMapperSampleJobFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testJobLaunch(@Autowired Job job) throws Exception { - // given - this.jobLauncherTestUtils.setJob(job); - + void testJobLaunch() throws Exception { // when this.jobLauncherTestUtils.launchJob(); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/CompositeItemWriterSampleFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/CompositeItemWriterSampleFunctionalTests.java index 84f8483112..5147b5fd05 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/CompositeItemWriterSampleFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/CompositeItemWriterSampleFunctionalTests.java @@ -28,7 +28,6 @@ import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.sample.domain.trade.Trade; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -62,8 +61,7 @@ public void setDataSource(DataSource dataSource) { } @Test - void testJobLaunch(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testJobLaunch() throws Exception { JdbcTestUtils.deleteFromTables(jdbcTemplate, "TRADE"); int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE"); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java index c9feb3646c..36ca641957 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/CustomerFilterJobFunctionalTests.java @@ -29,7 +29,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -82,8 +81,7 @@ void tearDown() { } @Test - void testFilterJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testFilterJob() throws Exception { JobExecution jobExecution = jobLauncherTestUtils.launchJob(); customers = Arrays.asList(new Customer("customer1", (credits.get("customer1"))), diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/DatabaseShutdownFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/DatabaseShutdownFunctionalTests.java index 9d509a04f1..91b117db5e 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/DatabaseShutdownFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/DatabaseShutdownFunctionalTests.java @@ -21,7 +21,6 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.BatchStatus; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.launch.JobOperator; import org.springframework.batch.test.JobLauncherTestUtils; @@ -57,8 +56,7 @@ class DatabaseShutdownFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testLaunchJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testLaunchJob() throws Exception { JobExecution jobExecution = jobLauncherTestUtils.launchJob(); Thread.sleep(1000); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/DelegatingJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/DelegatingJobFunctionalTests.java index b15f617d3d..738edd8134 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/DelegatingJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/DelegatingJobFunctionalTests.java @@ -17,7 +17,6 @@ import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.sample.domain.person.PersonService; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -37,8 +36,7 @@ class DelegatingJobFunctionalTests { private PersonService personService; @Test - void testLaunchJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testLaunchJob() throws Exception { jobLauncherTestUtils.launchJob(); assertTrue(personService.getReturnedCount() > 0); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/FootballJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/FootballJobFunctionalTests.java index 7c91baf9c2..d0a1a4107a 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/FootballJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/FootballJobFunctionalTests.java @@ -19,7 +19,6 @@ import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; @@ -43,8 +42,7 @@ public void setDataSource(DataSource dataSource) { } @Test - void testLaunchJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testLaunchJob() throws Exception { JdbcTestUtils.deleteFromTables(jdbcTemplate, "PLAYERS", "GAMES", "PLAYER_SUMMARY"); jobLauncherTestUtils.launchJob(); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/GracefulShutdownFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/GracefulShutdownFunctionalTests.java index d48317077f..fae8a10893 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/GracefulShutdownFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/GracefulShutdownFunctionalTests.java @@ -21,7 +21,6 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.BatchStatus; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; @@ -58,8 +57,7 @@ class GracefulShutdownFunctionalTests { private JobOperator jobOperator; @Test - void testLaunchJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testLaunchJob() throws Exception { final JobParameters jobParameters = new JobParametersBuilder().addLong("timestamp", System.currentTimeMillis()) .toJobParameters(); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/GroovyJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/GroovyJobFunctionalTests.java index b3a6bdd089..bd58f285da 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/GroovyJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/GroovyJobFunctionalTests.java @@ -23,7 +23,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @@ -43,8 +42,7 @@ void removeOldData() throws IOException { } @Test - void testLaunchJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testLaunchJob() throws Exception { assertFalse(new File("target/groovyJob/output/files.zip").exists()); jobLauncherTestUtils.launchJob(); assertTrue(new File("target/groovyJob/output/files.zip").exists()); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/HeaderFooterSampleFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/HeaderFooterSampleFunctionalTests.java index 22104d214b..812cdc1644 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/HeaderFooterSampleFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/HeaderFooterSampleFunctionalTests.java @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -45,8 +44,7 @@ class HeaderFooterSampleFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testJob() throws Exception { this.jobLauncherTestUtils.launchJob(); BufferedReader inputReader = new BufferedReader(new FileReader(input.getFile())); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/HibernateFailureJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/HibernateFailureJobFunctionalTests.java index 08696693d0..fa1705636d 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/HibernateFailureJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/HibernateFailureJobFunctionalTests.java @@ -25,7 +25,6 @@ import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor; @@ -92,8 +91,7 @@ public void setDataSource(DataSource dataSource) { } @Test - void testLaunchJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testLaunchJob() throws Exception { validatePreConditions(); JobParameters params = new JobParametersBuilder().addString("key", "failureJob").toJobParameters(); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/LoopFlowSampleFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/LoopFlowSampleFunctionalTests.java index de51eff95c..a7b54e51d5 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/LoopFlowSampleFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/LoopFlowSampleFunctionalTests.java @@ -17,7 +17,6 @@ import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.sample.domain.trade.internal.ItemTrackingTradeItemWriter; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -45,8 +44,7 @@ class LoopFlowSampleFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testJobLaunch(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testJobLaunch() throws Exception { this.jobLauncherTestUtils.launchJob(); // items processed = items read + 2 exceptions assertEquals(10, itemWriter.getItems().size()); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/MailJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/MailJobFunctionalTests.java index 7849e0a3a6..d6d2896855 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/MailJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/MailJobFunctionalTests.java @@ -25,7 +25,6 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.ExitStatus; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.sample.domain.mail.internal.TestMailErrorHandler; import org.springframework.batch.sample.domain.mail.internal.TestMailSender; @@ -96,8 +95,7 @@ void after() { } @Test - void testSkip(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testSkip() throws Exception { this.createUsers(new Object[][] { USER1, USER2_SKIP, USER3, USER4_SKIP, USER5, USER6, USER7, USER8 }); JobExecution jobExecution = jobLauncherTestUtils.launchJob(); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/MultilineJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/MultilineJobFunctionalTests.java index 2bd4169224..dada9ca793 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/MultilineJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/MultilineJobFunctionalTests.java @@ -19,7 +19,6 @@ import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; @@ -44,8 +43,7 @@ class MultilineJobFunctionalTests { private final Resource output = new FileSystemResource("target/test-outputs/20070122.testStream.multilineStep.txt"); @Test - void testJobLaunch(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testJobLaunch() throws Exception { this.jobLauncherTestUtils.launchJob(); assertEquals(EXPECTED_RESULT, StringUtils.replace(IOUtils.toString(output.getInputStream(), "UTF-8"), System.getProperty("line.separator"), "")); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/MultilineOrderJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/MultilineOrderJobFunctionalTests.java index 041faa003d..9fdf297e63 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/MultilineOrderJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/MultilineOrderJobFunctionalTests.java @@ -22,7 +22,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; @@ -41,8 +40,7 @@ class MultilineOrderJobFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testJobLaunch(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testJobLaunch() throws Exception { this.jobLauncherTestUtils.launchJob(); Path expectedFile = new ClassPathResource(EXPECTED).getFile().toPath(); Path actualFile = new FileSystemResource(ACTUAL).getFile().toPath(); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/ParallelJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/ParallelJobFunctionalTests.java index 6639d49a59..b5f7a4a297 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/ParallelJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/ParallelJobFunctionalTests.java @@ -21,7 +21,6 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.BatchStatus; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -46,8 +45,7 @@ public void setDataSource(DataSource dataSource) { } @Test - void testLaunchJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testLaunchJob() throws Exception { int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING"); JobExecution execution = jobLauncherTestUtils.launchJob(); int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING"); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/PartitionFileJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/PartitionFileJobFunctionalTests.java index 8012d73c44..c638483cdb 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/PartitionFileJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/PartitionFileJobFunctionalTests.java @@ -24,7 +24,6 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.BatchStatus; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemReader; @@ -64,8 +63,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws * Check the resulting credits correspond to inputs increased by fixed amount. */ @Test - void testUpdateCredit(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testUpdateCredit() throws Exception { assertTrue(applicationContext.containsBeanDefinition("outputTestReader"), "Define a prototype bean called 'outputTestReader' to check the output"); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/PartitionJdbcJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/PartitionJdbcJobFunctionalTests.java index d405b6c4f8..60c9131015 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/PartitionJdbcJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/PartitionJdbcJobFunctionalTests.java @@ -24,7 +24,6 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.BatchStatus; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemReader; @@ -64,8 +63,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws * Check the resulting credits correspond to inputs increased by fixed amount. */ @Test - void testUpdateCredit(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testUpdateCredit() throws Exception { assertTrue(applicationContext.containsBeanDefinition("outputTestReader"), "Define a prototype bean called 'outputTestReader' to check the output"); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFileSampleFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFileSampleFunctionalTests.java index c4aad25720..8a1ef4fec0 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFileSampleFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFileSampleFunctionalTests.java @@ -16,7 +16,6 @@ package org.springframework.batch.sample; -import java.io.File; import java.nio.file.Files; import java.nio.file.Path; @@ -24,7 +23,6 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.BatchStatus; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.item.Chunk; @@ -54,8 +52,7 @@ class RestartFileSampleFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void runTest(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void runTest() throws Exception { JobParameters jobParameters = jobLauncherTestUtils.getUniqueJobParameters(); JobExecution je1 = jobLauncherTestUtils.launchJob(jobParameters); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFunctionalTests.java index a3d84b11b1..0c059afb55 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RestartFunctionalTests.java @@ -21,7 +21,6 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.BatchStatus; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.converter.DefaultJobParametersConverter; import org.springframework.batch.support.PropertiesConverter; @@ -70,8 +69,7 @@ void onTearDown() { * @throws Exception */ @Test - void testLaunchJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testLaunchJob() throws Exception { int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE"); JobExecution jobExecution = runJobForRestartTest(); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RetrySampleFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RetrySampleFunctionalTests.java index eff8410d8d..bd2a6b3890 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RetrySampleFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RetrySampleFunctionalTests.java @@ -17,7 +17,6 @@ import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.sample.domain.trade.internal.GeneratingTradeItemReader; import org.springframework.batch.sample.support.RetrySampleItemWriter; import org.springframework.batch.test.JobLauncherTestUtils; @@ -48,8 +47,7 @@ class RetrySampleFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testLaunchJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testLaunchJob() throws Exception { this.jobLauncherTestUtils.launchJob(); // items processed = items read + 2 exceptions assertEquals(itemGenerator.getLimit() + 2, itemProcessor.getCounter()); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/TaskletJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/TaskletJobFunctionalTests.java index 112f61e521..c6454d2602 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/TaskletJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/TaskletJobFunctionalTests.java @@ -19,7 +19,6 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.BatchStatus; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.scope.context.ChunkContext; @@ -37,8 +36,7 @@ class TaskletJobFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testLaunchJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testLaunchJob() throws Exception { JobExecution jobExecution = jobLauncherTestUtils .launchJob(new JobParametersBuilder().addString("value", "foo").toJobParameters()); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java index 46fe95975e..015c69efca 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/TradeJobFunctionalTests.java @@ -30,7 +30,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.sample.domain.trade.Trade; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -82,8 +81,7 @@ void tearDown() { } @Test - void testLaunchJob(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); + void testLaunchJob() throws Exception { this.jobLauncherTestUtils.launchJob(); customers = Arrays.asList(new Customer("customer1", (credits.get("customer1") - 98.34)), diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/AbstractIoSampleTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/AbstractIoSampleTests.java index 4dd8de1210..ba38403fde 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/AbstractIoSampleTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/AbstractIoSampleTests.java @@ -21,7 +21,6 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.BatchStatus; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; @@ -65,9 +64,7 @@ abstract class AbstractIoSampleTests { * Check the resulting credits correspond to inputs increased by fixed amount. */ @Test - void testUpdateCredit(@Autowired Job job) throws Exception { - this.jobLauncherTestUtils.setJob(job); - + void testUpdateCredit() throws Exception { open(reader); List inputs = getCredits(reader); close(reader); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiLineFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiLineFunctionalTests.java index 68caf23ff6..97e3991e88 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiLineFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiLineFunctionalTests.java @@ -22,7 +22,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; @@ -49,10 +48,7 @@ class MultiLineFunctionalTests { * Output should be the same as input */ @Test - void testJob(@Autowired Job job) throws Exception { - // given - this.jobLauncherTestUtils.setJob(job); - + void testJob() throws Exception { // when this.jobLauncherTestUtils.launchJob(); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiRecordTypeFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiRecordTypeFunctionalTests.java index db262fa242..bce410951e 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiRecordTypeFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/iosample/MultiRecordTypeFunctionalTests.java @@ -22,7 +22,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.springframework.batch.core.Job; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; @@ -49,10 +48,7 @@ class MultiRecordTypeFunctionalTests { * Output should be the same as input */ @Test - void testJob(@Autowired Job job) throws Exception { - // given - this.jobLauncherTestUtils.setJob(job); - + void testJob() throws Exception { // when jobLauncherTestUtils.launchJob(); diff --git a/spring-batch-samples/src/test/resources/job-runner-context.xml b/spring-batch-samples/src/test/resources/job-runner-context.xml index a2dc389565..f93e9127d0 100644 --- a/spring-batch-samples/src/test/resources/job-runner-context.xml +++ b/spring-batch-samples/src/test/resources/job-runner-context.xml @@ -7,4 +7,5 @@ + diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessor.java b/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessor.java new file mode 100644 index 0000000000..56c3f98206 --- /dev/null +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessor.java @@ -0,0 +1,49 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.test.context; + +import org.springframework.batch.core.Job; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.BeanPostProcessor; + +/** + * {@link BeanPostProcessor} implementation that injects a job bean into + * {@link JobLauncherTestUtils} if there is a unique job bean. + * + * @author Henning Pöttker + * @since 5.0 + */ +public class BatchTestContextBeanPostProcessor implements BeanPostProcessor { + + private ObjectProvider jobProvider; + + @Autowired + public void setJobProvider(ObjectProvider jobProvider) { + this.jobProvider = jobProvider; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof JobLauncherTestUtils jobLauncherTestUtils) { + jobProvider.ifUnique(jobLauncherTestUtils::setJob); + } + return bean; + } + +} diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextCustomizer.java b/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextCustomizer.java index 2285b2fe51..7608b88500 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextCustomizer.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2018-2021 the original author or authors. + * Copyright 2018-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,8 @@ public class BatchTestContextCustomizer implements ContextCustomizer { private static final String JOB_REPOSITORY_TEST_UTILS_BEAN_NAME = "jobRepositoryTestUtils"; + private static final String BATCH_TEST_CONTEXT_BEAN_POST_PROCESSOR_BEAN_NAME = "batchTestContextBeanPostProcessor"; + @Override public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) { ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); @@ -50,6 +52,8 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte new RootBeanDefinition(JobLauncherTestUtils.class)); registry.registerBeanDefinition(JOB_REPOSITORY_TEST_UTILS_BEAN_NAME, new RootBeanDefinition(JobRepositoryTestUtils.class)); + registry.registerBeanDefinition(BATCH_TEST_CONTEXT_BEAN_POST_PROCESSOR_BEAN_NAME, + new RootBeanDefinition(BatchTestContextBeanPostProcessor.class)); } @Override diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java index 3bdc4a303b..f0562064b9 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit4Tests.java @@ -69,9 +69,6 @@ public class SpringBatchTestJUnit4Tests { @Autowired private ItemReader jobScopedItemReader; - @Autowired - private Job jobUnderTest; - @Before public void setUp() { this.jobRepositoryTestUtils.removeJobExecutions(); @@ -105,9 +102,6 @@ public void testJobScopedItemReader() throws Exception { @Test public void testJob() throws Exception { - // given - this.jobLauncherTestUtils.setJob(this.jobUnderTest); - // when JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(); diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java index e588876c96..1944d61735 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/SpringBatchTestJUnit5Tests.java @@ -19,7 +19,6 @@ import javax.sql.DataSource; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -72,8 +71,7 @@ public class SpringBatchTestJUnit5Tests { private ItemReader jobScopedItemReader; @BeforeEach - void setup(@Autowired Job jobUnderTest) { - this.jobLauncherTestUtils.setJob(jobUnderTest); + void setup() { this.jobRepositoryTestUtils.removeJobExecutions(); } diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessorTest.java b/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessorTest.java new file mode 100644 index 0000000000..179c46ce50 --- /dev/null +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextBeanPostProcessorTest.java @@ -0,0 +1,118 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.test.context; + +import javax.sql.DataSource; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; +import org.springframework.jdbc.support.JdbcTransactionManager; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * @author Henning Pöttker + */ +class BatchTestContextBeanPostProcessorTest { + + private GenericApplicationContext applicationContext; + + @BeforeEach + void setUp() { + applicationContext = new AnnotationConfigApplicationContext(BatchConfiguration.class); + applicationContext.registerBean(JobLauncherTestUtils.class); + } + + @AfterEach + void tearDown() { + if (applicationContext != null) { + applicationContext.close(); + } + } + + @Test + void testContextWithoutJobBean() { + var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class); + assertNotNull(jobLauncherTestUtils); + assertNull(jobLauncherTestUtils.getJob()); + } + + @Test + void testContextWithUniqueJobBean() { + applicationContext.registerBean(MockJob.class); + var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class); + assertNotNull(jobLauncherTestUtils.getJob()); + } + + @Test + void testContextWithTwoJobBeans() { + applicationContext.registerBean("jobA", MockJob.class); + applicationContext.registerBean("jobB", MockJob.class); + var jobLauncherTestUtils = applicationContext.getBean(JobLauncherTestUtils.class); + assertNotNull(jobLauncherTestUtils); + assertNull(jobLauncherTestUtils.getJob()); + } + + static class MockJob implements Job { + + @Override + public String getName() { + return "name"; + } + + @Override + public void execute(JobExecution execution) { + } + + } + + @Configuration + @EnableBatchProcessing + static class BatchConfiguration { + + @Bean + DataSource dataSource() { + return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL) + .addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql") + .addScript("/org/springframework/batch/core/schema-hsqldb.sql").build(); + } + + @Bean + JdbcTransactionManager transactionManager(DataSource dataSource) { + return new JdbcTransactionManager(dataSource); + } + + @Bean + BatchTestContextBeanPostProcessor beanPostProcessor() { + return new BatchTestContextBeanPostProcessor(); + } + + } + +} diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerTests.java index d56235a9f1..c948d7c10a 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerTests.java @@ -46,6 +46,7 @@ void testCustomizeContext() { // then assertTrue(context.containsBean("jobLauncherTestUtils")); assertTrue(context.containsBean("jobRepositoryTestUtils")); + assertTrue(context.containsBean("batchTestContextBeanPostProcessor")); } @Test diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/observability/ObservabilitySampleStepTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/observability/ObservabilitySampleStepTests.java index d605fc8323..6d5cc7d162 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/observability/ObservabilitySampleStepTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/observability/ObservabilitySampleStepTests.java @@ -22,10 +22,8 @@ import io.micrometer.tracing.test.SampleTestRunner; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.springframework.batch.core.ExitStatus; -import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.observability.BatchMetrics; @@ -58,11 +56,6 @@ protected ObservationRegistry createObservationRegistry() { return BatchMetrics.observationRegistry; } - @BeforeEach - void setup(@Autowired Job job) { - this.jobLauncherTestUtils.setJob(job); - } - @AfterEach @Override protected void closeMeterRegistry() {