- * A typical usage of this annotation with JUnit 4 is like: + * A typical usage of this annotation with JUnit 4 is like the following: * *
* @RunWith(SpringRunner.class)
@@ -66,7 +64,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
* @Before
* public void setup() {
* this.jobRepositoryTestUtils.removeJobExecutions();
- * this.jobLauncherTestUtils.setJob(this.jobUnderTest);
+ * this.jobLauncherTestUtils.setJob(this.jobUnderTest); // this is optional if the job is unique
* }
*
* @Test
@@ -84,9 +82,9 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
* }
*
*
- * For JUnit 5, this annotation can be used without having to manually register the
+ * For JUnit 5, this annotation can be used without manually registering the
* {@link SpringExtension} since {@code @SpringBatchTest} is meta-annotated with
- * {@code @ExtendWith(SpringExtension.class)}:
+ * {@code @ExtendWith(SpringExtension.class)}. Here is an example:
*
*
* @SpringBatchTest
@@ -101,7 +99,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
*
* @BeforeEach
* public void setup(@Autowired Job jobUnderTest) {
- * this.jobLauncherTestUtils.setJob(jobUnderTest);
+ * this.jobLauncherTestUtils.setJob(jobUnderTest); // this is optional if the job is unique
* this.jobRepositoryTestUtils.removeJobExecutions();
* }
*
@@ -124,6 +122,12 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
* is the job under test, then this annotation will set that job in the
* {@link JobLauncherTestUtils} automatically.
*
+ * The test context must contain a JobRepository and a
+ * JobLauncher beans for this annotation to properly set up test utilities.
+ * In the previous example, the imported configuration class
+ * MyBatchJobConfiguration is expected to have such beans defined in it (or
+ * imported from another configuration class).
+ *
* @author Mahmoud Ben Hassine
* @since 4.1
* @see JobLauncherTestUtils
diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/JobLauncherTestUtilsTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/JobLauncherTestUtilsTests.java
index 3a3bb48d2..fb46c44a1 100644
--- a/spring-batch-test/src/test/java/org/springframework/batch/test/JobLauncherTestUtilsTests.java
+++ b/spring-batch-test/src/test/java/org/springframework/batch/test/JobLauncherTestUtilsTests.java
@@ -25,12 +25,12 @@ import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.job.builder.JobBuilder;
+import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -97,9 +97,11 @@ class JobLauncherTestUtilsTests {
}
@Bean
- public JobLauncherTestUtils testUtils(Job jobUnderTest) {
+ public JobLauncherTestUtils testUtils(Job jobUnderTest, JobRepository jobRepository, JobLauncher jobLauncher) {
JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
jobLauncherTestUtils.setJob(jobUnderTest);
+ jobLauncherTestUtils.setJobRepository(jobRepository);
+ jobLauncherTestUtils.setJobLauncher(jobLauncher);
return jobLauncherTestUtils;
}
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 7b548a4f5..e26410dcb 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
@@ -15,14 +15,13 @@
*/
package org.springframework.batch.test;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
+
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
@@ -33,6 +32,7 @@ import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.JobBuilder;
+import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.Chunk;
@@ -49,6 +49,8 @@ import org.springframework.lang.Nullable;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.transaction.PlatformTransactionManager;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
@SpringJUnitConfig
class StepScopeAnnotatedListenerIntegrationTests {
@@ -104,8 +106,11 @@ class StepScopeAnnotatedListenerIntegrationTests {
private PlatformTransactionManager transactionManager;
@Bean
- JobLauncherTestUtils jobLauncherTestUtils() {
- return new JobLauncherTestUtils();
+ JobLauncherTestUtils jobLauncherTestUtils(JobRepository jobRepository, JobLauncher jobLauncher) {
+ JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
+ jobLauncherTestUtils.setJobRepository(jobRepository);
+ jobLauncherTestUtils.setJobLauncher(jobLauncher);
+ return jobLauncherTestUtils;
}
@Bean
diff --git a/spring-batch-test/src/test/resources/job-runner-context.xml b/spring-batch-test/src/test/resources/job-runner-context.xml
index 5751979ee..96fa9648e 100644
--- a/spring-batch-test/src/test/resources/job-runner-context.xml
+++ b/spring-batch-test/src/test/resources/job-runner-context.xml
@@ -4,7 +4,9 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
-
-
+
+
+
+