From c506a499881284cdd033c041c39bd302f58fd147 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Sat, 20 Aug 2022 12:00:08 +0200 Subject: [PATCH] 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 --- .../src/main/asciidoc/testing.adoc | 45 ++++++++----------- .../batch/sample/AMQPJobFunctionalTests.java | 11 +++-- ...WrapperMapperSampleJobFunctionalTests.java | 11 ++++- ...positeItemWriterSampleFunctionalTests.java | 4 +- .../CustomerFilterJobFunctionalTests.java | 5 ++- .../DatabaseShutdownFunctionalTests.java | 5 ++- .../sample/DelegatingJobFunctionalTests.java | 6 ++- .../sample/FootballJobFunctionalTests.java | 5 ++- .../GracefulShutdownFunctionalTests.java | 5 ++- .../sample/GroovyJobFunctionalTests.java | 5 ++- .../HeaderFooterSampleFunctionalTests.java | 7 ++- .../HibernateFailureJobFunctionalTests.java | 4 +- .../sample/LoopFlowSampleFunctionalTests.java | 7 ++- .../batch/sample/MailJobFunctionalTests.java | 4 +- .../sample/MultilineJobFunctionalTests.java | 6 ++- .../MultilineOrderJobFunctionalTests.java | 7 ++- .../sample/ParallelJobFunctionalTests.java | 4 +- .../PartitionFileJobFunctionalTests.java | 4 +- .../PartitionJdbcJobFunctionalTests.java | 4 +- .../RemoteChunkingJobFunctionalTests.java | 6 ++- .../RemotePartitioningJobFunctionalTests.java | 6 ++- .../RestartFileSampleFunctionalTests.java | 4 +- .../batch/sample/RestartFunctionalTests.java | 4 +- .../sample/RetrySampleConfigurationTests.java | 8 +++- .../sample/RetrySampleFunctionalTests.java | 8 +++- .../sample/TaskletJobFunctionalTests.java | 4 +- .../batch/sample/TradeJobFunctionalTests.java | 7 ++- .../iosample/AbstractIoSampleTests.java | 5 ++- .../iosample/MultiLineFunctionalTests.java | 13 +++++- .../MultiRecordTypeFunctionalTests.java | 11 ++++- .../batch/test/JobLauncherTestUtils.java | 3 +- .../batch/test/context/SpringBatchTest.java | 23 +++++----- .../batch/test/AbstractSampleJobTests.java | 5 ++- .../test/SpringBatchTestJUnit4Tests.java | 8 +++- .../test/SpringBatchTestJUnit5Tests.java | 3 +- ...copeAnnotatedListenerIntegrationTests.java | 7 ++- .../ObservabilitySampleStepTests.java | 7 +++ 37 files changed, 195 insertions(+), 86 deletions(-) diff --git a/spring-batch-docs/src/main/asciidoc/testing.adoc b/spring-batch-docs/src/main/asciidoc/testing.adoc index d005d256b..ea4b3b534 100644 --- a/spring-batch-docs/src/main/asciidoc/testing.adoc +++ b/spring-batch-docs/src/main/asciidoc/testing.adoc @@ -22,20 +22,15 @@ approach. For the unit test to run a batch job, the framework must load the job's `ApplicationContext`. Two annotations are used to trigger this behavior: -* `@RunWith(SpringJUnit4ClassRunner.class)` indicates that the class should use Spring's +* `@SpringJUnitConfig` indicates that the class should use Spring's JUnit facilities -* `@ContextConfiguration(...)` indicates which resources to configure the -`ApplicationContext` with. +* `@SpringBatchTest` injects Spring Batch test utilities (such as the +`JobLauncherTestUtils` and `JobRepositoryTestUtils`) in the test context -Starting from v4.1, it is also possible to inject Spring Batch test utilities -(such as the `JobLauncherTestUtils` and `JobRepositoryTestUtils`) in the test context -by using the `@SpringBatchTest` annotation. - -NOTE: Note that `JobLauncherTestUtils` requires a `Job` bean and that -`JobRepositoryTestUtils` requires a `DataSource` bean. Since `@SpringBatchTest` -registers a `JobLauncherTestUtils` and a `JobRepositoryTestUtils` in the test +NOTE: Note that `JobRepositoryTestUtils` requires a `DataSource` bean. Since +`@SpringBatchTest` registers a `JobRepositoryTestUtils` in the test context, it is expected that the test context contains a single autowire candidate -for a `Job` and a `DataSource` (either a single bean definition or one that is +for a `DataSource` (either a single bean definition or one that is annotated with `org.springframework.context.annotation.Primary`). [role="javaContent"] @@ -45,8 +40,7 @@ The following Java example shows the annotations in use: [source, java, role="javaContent"] ---- @SpringBatchTest -@RunWith(SpringRunner.class) -@ContextConfiguration(classes=SkipSampleConfiguration.class) +@SpringJUnitConfig(SkipSampleConfiguration.class) public class SkipSampleFunctionalTests { ... } ---- @@ -57,8 +51,7 @@ The following XML example shows the annotations in use: [source, java, role="xmlContent"] ---- @SpringBatchTest -@RunWith(SpringRunner.class) -@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", +@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/jobs/skipSampleJob.xml" }) public class SkipSampleFunctionalTests { ... } ---- @@ -81,14 +74,13 @@ about the `Job` run. In the following case, the test verifies that the `Job` end a status of `COMPLETED`. [role="xmlContent"] -The following listing shows the example in XML: +The following listing shows an example with JUnit 5 in XML configuration style: .XML Based Configuration [source, java, role="xmlContent"] ---- @SpringBatchTest -@RunWith(SpringRunner.class) -@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", +@SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/jobs/skipSampleJob.xml" }) public class SkipSampleFunctionalTests { @@ -103,7 +95,8 @@ public class SkipSampleFunctionalTests { } @Test - public void testJob() throws Exception { + public void testJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); simpleJdbcTemplate.update("delete from CUSTOMER"); for (int i = 1; i <= 10; i++) { simpleJdbcTemplate.update("insert into CUSTOMER values (?, 0, ?, 100000)", @@ -119,14 +112,13 @@ public class SkipSampleFunctionalTests { ---- [role="javaContent"] -The following listing shows the example in Java: +The following listing shows an example with JUnit 5 in Java configuration style: .Java Based Configuration [source, java, role="javaContent"] ---- @SpringBatchTest -@RunWith(SpringRunner.class) -@ContextConfiguration(classes=SkipSampleConfiguration.class) +@SpringJUnitConfig(SkipSampleConfiguration.class) public class SkipSampleFunctionalTests { @Autowired @@ -140,7 +132,8 @@ public class SkipSampleFunctionalTests { } @Test - public void testJob() throws Exception { + public void testJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); simpleJdbcTemplate.update("delete from CUSTOMER"); for (int i = 1; i <= 10; i++) { simpleJdbcTemplate.update("insert into CUSTOMER values (?, 0, ?, 100000)", @@ -186,10 +179,9 @@ context for each test method, as the following example shows: [source, java] ---- -@ContextConfiguration +@SpringJUnitConfig @TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class }) -@RunWith(SpringRunner.class) public class StepScopeTestExecutionListenerIntegrationTests { // This component is defined step-scoped, so it cannot be injected unless @@ -228,8 +220,7 @@ example can be configured as follows: [source, java] ---- @SpringBatchTest -@RunWith(SpringRunner.class) -@ContextConfiguration +@SpringJUnitConfig public class StepScopeTestExecutionListenerIntegrationTests { // This component is defined step-scoped, so it cannot be injected unless 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 ff17b7796..609a02987 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 @@ -18,6 +18,8 @@ package org.springframework.batch.sample; import static org.junit.jupiter.api.Assertions.assertTrue; 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; @@ -48,12 +50,15 @@ class AMQPJobFunctionalTests { private JobExplorer jobExplorer; @Test - void testLaunchJob() throws Exception { - - jobLauncherTestUtils.launchJob(); + void testLaunchJob(@Autowired Job job) throws Exception { + // given + this.jobLauncherTestUtils.setJob(job); + this.jobLauncherTestUtils.launchJob(); + // when int count = jobExplorer.getJobInstances("amqp-example-job", 0, 1).size(); + // then assertTrue(count > 0); } 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 b5d16a584..8595d981d 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 @@ -17,6 +17,8 @@ package org.springframework.batch.sample; 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; @@ -29,10 +31,15 @@ class BeanWrapperMapperSampleJobFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testJobLaunch() throws Exception { + void testJobLaunch(@Autowired Job job) throws Exception { + // given + this.jobLauncherTestUtils.setJob(job); - jobLauncherTestUtils.launchJob(); + // when + this.jobLauncherTestUtils.launchJob(); + // then + // FIXME no assertions? } } 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 ce31eda86..b5c20462f 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 @@ -27,6 +27,7 @@ import javax.sql.DataSource; 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; @@ -60,7 +61,8 @@ class CompositeItemWriterSampleFunctionalTests { } @Test - void testJobLaunch() throws Exception { + void testJobLaunch(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 69110d385..43ff6ddaf 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 @@ -30,6 +30,8 @@ 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.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -80,7 +82,8 @@ class CustomerFilterJobFunctionalTests { } @Test - void testFilterJob() throws Exception { + void testFilterJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 a85443cd0..a409a26fa 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 @@ -25,6 +25,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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; @@ -53,8 +54,8 @@ class DatabaseShutdownFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testLaunchJob() throws Exception { - + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 4ca1283e4..82fe410b3 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 @@ -19,6 +19,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; 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; @@ -35,8 +37,8 @@ class DelegatingJobFunctionalTests { private PersonService personService; @Test - void testLaunchJob() throws Exception { - + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 283cf70db..17921faae 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 @@ -20,6 +20,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import javax.sql.DataSource; 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; @@ -41,7 +43,8 @@ class FootballJobFunctionalTests { } @Test - void testLaunchJob() throws Exception { + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 9f0b9d269..30c09e31b 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 @@ -24,6 +24,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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; @@ -54,8 +55,8 @@ class GracefulShutdownFunctionalTests { private JobOperator jobOperator; @Test - void testLaunchJob() throws Exception { - + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 09afcd451..516eea438 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 @@ -25,6 +25,8 @@ import java.io.IOException; import org.apache.commons.io.FileUtils; 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; @@ -41,7 +43,8 @@ class GroovyJobFunctionalTests { } @Test - void testLaunchJob() throws Exception { + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 ec55a4510..dcd23da78 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 @@ -21,6 +21,8 @@ import java.io.BufferedReader; import java.io.FileReader; 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; @@ -43,8 +45,9 @@ class HeaderFooterSampleFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testJob() throws Exception { - jobLauncherTestUtils.launchJob(); + void testJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); + this.jobLauncherTestUtils.launchJob(); BufferedReader inputReader = new BufferedReader(new FileReader(input.getFile())); BufferedReader outputReader = new BufferedReader(new FileReader(output.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 d429200d9..22e600fc7 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 @@ -28,6 +28,7 @@ import javax.sql.DataSource; 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; @@ -89,7 +90,8 @@ class HibernateFailureJobFunctionalTests { } @Test - void testLaunchJob() throws Exception { + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 082cc22c8..3025970b9 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 @@ -18,6 +18,8 @@ package org.springframework.batch.sample; import static org.junit.jupiter.api.Assertions.assertEquals; 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; @@ -40,8 +42,9 @@ class LoopFlowSampleFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testJobLaunch() throws Exception { - jobLauncherTestUtils.launchJob(); + void testJobLaunch(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); + 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 4abc32f5e..e23dda354 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 @@ -26,6 +26,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; 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; @@ -93,7 +94,8 @@ class MailJobFunctionalTests { } @Test - void testSkip() throws Exception { + void testSkip(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 c38a896f5..2bd416922 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,6 +19,7 @@ package org.springframework.batch.sample; 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; @@ -43,8 +44,9 @@ class MultilineJobFunctionalTests { private final Resource output = new FileSystemResource("target/test-outputs/20070122.testStream.multilineStep.txt"); @Test - void testJobLaunch() throws Exception { - jobLauncherTestUtils.launchJob(); + void testJobLaunch(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); + 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 e900de909..07aa65c61 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 @@ -19,6 +19,8 @@ package org.springframework.batch.sample; import static org.springframework.batch.test.AssertFile.assertFileEquals; 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; @@ -37,8 +39,9 @@ class MultilineOrderJobFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testJobLaunch() throws Exception { - jobLauncherTestUtils.launchJob(); + void testJobLaunch(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); + this.jobLauncherTestUtils.launchJob(); assertFileEquals(new ClassPathResource(EXPECTED), new FileSystemResource(ACTUAL)); } 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 024af43b9..ee898848b 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 @@ -22,6 +22,7 @@ import javax.sql.DataSource; 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.test.jdbc.JdbcTestUtils; import org.springframework.batch.test.JobLauncherTestUtils; @@ -44,7 +45,8 @@ class ParallelJobFunctionalTests { } @Test - void testLaunchJob() throws Exception { + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 123c0557c..ea463ad9d 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 @@ -26,6 +26,7 @@ import java.util.Set; 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; @@ -62,7 +63,8 @@ class PartitionFileJobFunctionalTests implements ApplicationContextAware { * Check the resulting credits correspond to inputs increased by fixed amount. */ @Test - void testUpdateCredit() throws Exception { + void testUpdateCredit(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 7adeaffae..424e7e9d2 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 @@ -26,6 +26,7 @@ import java.util.Set; 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; @@ -62,7 +63,8 @@ class PartitionJdbcJobFunctionalTests implements ApplicationContextAware { * Check the resulting credits correspond to inputs increased by fixed amount. */ @Test - void testUpdateCredit() throws Exception { + void testUpdateCredit(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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/RemoteChunkingJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RemoteChunkingJobFunctionalTests.java index 144ded649..ef818bdcf 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RemoteChunkingJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RemoteChunkingJobFunctionalTests.java @@ -23,6 +23,7 @@ import org.junit.jupiter.api.BeforeEach; 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.config.JobRunnerConfiguration; import org.springframework.batch.sample.remotechunking.ManagerConfiguration; @@ -69,7 +70,10 @@ class RemoteChunkingJobFunctionalTests { } @Test - void testRemoteChunkingJob() throws Exception { + void testRemoteChunkingJob(@Autowired Job job) throws Exception { + // given + this.jobLauncherTestUtils.setJob(job); + // when JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RemotePartitioningJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RemotePartitioningJobFunctionalTests.java index 969e6c5d4..8beedffa9 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RemotePartitioningJobFunctionalTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RemotePartitioningJobFunctionalTests.java @@ -25,6 +25,7 @@ import org.junit.jupiter.api.BeforeEach; 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.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -75,7 +76,10 @@ public abstract class RemotePartitioningJobFunctionalTests { } @Test - void testRemotePartitioningJob() throws Exception { + void testRemotePartitioningJob(@Autowired Job job) throws Exception { + // given + this.jobLauncherTestUtils.setJob(job); + // when JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(); 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 70518dd4d..3e0171770 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 @@ -22,6 +22,7 @@ import java.util.List; 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; @@ -49,7 +50,8 @@ class RestartFileSampleFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void runTest() throws Exception { + void runTest(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 ef826f45b..920f36995 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 @@ -23,6 +23,7 @@ import javax.sql.DataSource; 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; @@ -67,7 +68,8 @@ class RestartFunctionalTests { * @throws Exception */ @Test - void testLaunchJob() throws Exception { + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "TRADE"); JobExecution jobExecution = runJobForRestartTest(); diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RetrySampleConfigurationTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RetrySampleConfigurationTests.java index 02020fee2..c5b36623b 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/RetrySampleConfigurationTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RetrySampleConfigurationTests.java @@ -18,6 +18,8 @@ package org.springframework.batch.sample; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; + +import org.springframework.batch.core.Job; import org.springframework.batch.sample.config.DataSourceConfiguration; import org.springframework.batch.sample.config.JobRunnerConfiguration; import org.springframework.batch.sample.config.RetrySampleConfiguration; @@ -32,6 +34,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; * * @author Robert Kasanicky * @author Dave Syer + * @author Mahmoud Ben Hassine */ @SpringJUnitConfig( classes = { DataSourceConfiguration.class, RetrySampleConfiguration.class, JobRunnerConfiguration.class }) @@ -47,8 +50,9 @@ class RetrySampleConfigurationTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testLaunchJob() throws Exception { - jobLauncherTestUtils.launchJob(); + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); + 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/RetrySampleFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/RetrySampleFunctionalTests.java index 8c5cbc07e..a25571b5d 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 @@ -18,6 +18,8 @@ package org.springframework.batch.sample; import static org.junit.jupiter.api.Assertions.assertEquals; 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; @@ -28,6 +30,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; * Checks that expected number of items have been processed. * * @author Robert Kasanicky + * @author Mahmoud Ben Hassine */ @SpringJUnitConfig( locations = { "/simple-job-launcher-context.xml", "/jobs/retrySample.xml", "/job-runner-context.xml" }) @@ -43,8 +46,9 @@ class RetrySampleFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testLaunchJob() throws Exception { - jobLauncherTestUtils.launchJob(); + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); + 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 ac2b85e47..78d60c102 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 @@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; 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; @@ -35,7 +36,8 @@ class TaskletJobFunctionalTests { private JobLauncherTestUtils jobLauncherTestUtils; @Test - void testLaunchJob() throws Exception { + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); 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 64d978bf9..b146c8fb0 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 @@ -32,6 +32,8 @@ 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.sample.domain.trade.Trade; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -81,8 +83,9 @@ class TradeJobFunctionalTests { } @Test - void testLaunchJob() throws Exception { - jobLauncherTestUtils.launchJob(); + void testLaunchJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); + this.jobLauncherTestUtils.launchJob(); customers = Arrays.asList(new Customer("customer1", (credits.get("customer1") - 98.34)), new Customer("customer2", (credits.get("customer2") - 18.12 - 12.78)), 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 684c85a56..8b77be2f3 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 @@ -23,6 +23,7 @@ import java.util.List; 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; @@ -46,6 +47,7 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution * to parse the outputs. * * @author Robert Kasanicky + * @author Mahmoud Ben Hassine */ @SpringJUnitConfig( locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml", "/jobs/ioSampleJob.xml" }) @@ -62,7 +64,8 @@ abstract class AbstractIoSampleTests { * Check the resulting credits correspond to inputs increased by fixed amount. */ @Test - void testUpdateCredit() throws Exception { + void testUpdateCredit(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); open(reader); List inputs = getCredits(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 2dece7f64..959578742 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 @@ -17,6 +17,8 @@ package org.springframework.batch.sample.iosample; import org.junit.jupiter.api.Test; + +import org.springframework.batch.core.Job; import org.springframework.batch.test.AssertFile; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -25,6 +27,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * @author Dan Garrette + * @author Mahmoud Ben Hassine * @since 2.0 */ @SpringJUnitConfig( @@ -42,8 +45,14 @@ class MultiLineFunctionalTests { * Output should be the same as input */ @Test - void testJob() throws Exception { - jobLauncherTestUtils.launchJob(); + void testJob(@Autowired Job job) throws Exception { + // given + this.jobLauncherTestUtils.setJob(job); + + // when + this.jobLauncherTestUtils.launchJob(); + + // then AssertFile.assertFileEquals(new FileSystemResource(INPUT_FILE), new FileSystemResource(OUTPUT_FILE)); } 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 4086c9134..86f4d5aaf 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 @@ -17,6 +17,8 @@ package org.springframework.batch.sample.iosample; import org.junit.jupiter.api.Test; + +import org.springframework.batch.core.Job; import org.springframework.batch.test.AssertFile; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -25,6 +27,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * @author Dan Garrette + * @author Mahmoud Ben Hassine * @since 2.0 */ @SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/jobs/iosample/multiRecordType.xml", @@ -42,8 +45,14 @@ class MultiRecordTypeFunctionalTests { * Output should be the same as input */ @Test - void testJob() throws Exception { + void testJob(@Autowired Job job) throws Exception { + // given + this.jobLauncherTestUtils.setJob(job); + + // when jobLauncherTestUtils.launchJob(); + + // then AssertFile.assertFileEquals(new FileSystemResource(INPUT_FILE), new FileSystemResource(OUTPUT_FILE)); } diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java b/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java index c54555ad2..9503527c8 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2020 the original author or authors. + * Copyright 2006-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. @@ -86,7 +86,6 @@ public class JobLauncherTestUtils { * The Job instance that can be manipulated (e.g. launched) in this utility. * @param job the {@link AbstractJob} to use */ - @Autowired public void setJob(Job job) { this.job = job; } diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/context/SpringBatchTest.java b/spring-batch-test/src/main/java/org/springframework/batch/test/context/SpringBatchTest.java index f77ca4ac7..1ba6c00dc 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/context/SpringBatchTest.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/context/SpringBatchTest.java @@ -60,9 +60,13 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; * @Autowired * private JobRepositoryTestUtils jobRepositoryTestUtils; * + * @Autowired + * private Job jobUnderTest; + * * @Before - * public void clearJobExecutions() { + * public void setup() { * this.jobRepositoryTestUtils.removeJobExecutions(); + * this.jobLauncherTestUtils.setJob(this.jobUnderTest); * } * * @Test @@ -86,7 +90,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; * *
  * @SpringBatchTest
- * @ContextConfiguration(classes = MyBatchJobConfiguration.class)
+ * @SpringJUnitConfig(MyBatchJobConfiguration.class)
  * public class MyBatchJobTests {
  *
  *    @Autowired
@@ -101,8 +105,9 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
  *    }
  *
  *    @Test
- *    public void testMyJob() throws Exception {
+ *    public void testMyJob(@Autowired Job jobUnderTest) throws Exception {
  *       // given
+ *       this.jobLauncherTestUtils.setJob(jobUnderTest);
  *       JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
  *
  *       // when
@@ -116,13 +121,11 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
  * 
* *

- * It should be noted that {@link JobLauncherTestUtils} requires a - * {@link org.springframework.batch.core.Job} bean and that {@link JobRepositoryTestUtils} - * requires a {@link javax.sql.DataSource} bean. Since this annotation registers a - * {@link JobLauncherTestUtils} and a {@link JobRepositoryTestUtils} in the test context, - * it is expected that the test context contains a single autowire candidate for a - * {@link org.springframework.batch.core.Job} and a {@link javax.sql.DataSource} (either a - * single bean definition or one that is annotated with + * It should be noted that {@link JobRepositoryTestUtils} requires a + * {@link javax.sql.DataSource} bean. Since this annotation registers a + * {@link JobRepositoryTestUtils} in the test context, it is expected that the test + * context contains a single autowire candidate for a {@link javax.sql.DataSource} (either + * a single bean definition or one that is annotated with * {@link org.springframework.context.annotation.Primary}). *

* diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java index 29a71e2b2..50ba4cfa0 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java @@ -24,6 +24,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.Job; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.test.sample.SampleTasklet; import org.springframework.beans.factory.annotation.Autowired; @@ -36,6 +37,7 @@ import org.springframework.test.jdbc.JdbcTestUtils; * This is an abstract test class. * * @author Dan Garrette + * @author Mahmoud Ben Hassine * @since 2.0 */ @SpringJUnitConfig(locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml" }) @@ -63,7 +65,8 @@ abstract class AbstractSampleJobTests { } @Test - void testJob() throws Exception { + void testJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchJob().getStatus()); this.verifyTasklet(1); this.verifyTasklet(2); 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 83088d2fd..338cf2aa2 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 @@ -1,5 +1,5 @@ /* - * Copyright 2018-2020 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. @@ -67,6 +67,9 @@ public class SpringBatchTestJUnit4Tests { @Autowired private ItemReader jobScopedItemReader; + @Autowired + private Job jobUnderTest; + @Before public void setUp() { this.jobRepositoryTestUtils.removeJobExecutions(); @@ -100,6 +103,9 @@ public class SpringBatchTestJUnit4Tests { @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 dbf203ad5..c82a34f1a 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 @@ -88,8 +88,9 @@ public class SpringBatchTestJUnit5Tests { } @Test - void testJob() throws Exception { + void testJob(@Autowired Job jobUnderTest) throws Exception { // given + this.jobLauncherTestUtils.setJob(jobUnderTest); JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters(); // when diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/StepScopeAnnotatedListenerIntegrationTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/StepScopeAnnotatedListenerIntegrationTests.java index f2b6f14bd..33bdbfaec 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/StepScopeAnnotatedListenerIntegrationTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/StepScopeAnnotatedListenerIntegrationTests.java @@ -53,9 +53,14 @@ class StepScopeAnnotatedListenerIntegrationTests { JobLauncherTestUtils jobLauncherTestUtils; @Test - void test() { + void test(@Autowired Job job) { + // given + this.jobLauncherTestUtils.setJob(job); + + // when JobExecution jobExecution = jobLauncherTestUtils.launchStep("step-under-test"); + // then assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); } 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 88aea8645..ece99f0c8 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 @@ -20,8 +20,10 @@ import io.micrometer.core.tck.MeterRegistryAssert; 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; @@ -44,6 +46,11 @@ class ObservabilitySampleStepTests extends SampleTestRunner { super(SampleRunnerConfig.builder().build(), BatchMetrics.observationRegistry, Metrics.globalRegistry); } + @BeforeEach + void setup(@Autowired Job job) { + this.jobLauncherTestUtils.setJob(job); + } + @AfterEach void clean() { Metrics.globalRegistry.clear();