Annotation Interface SpringBatchTest
@Target(TYPE)
@Retention(RUNTIME)
@Documented
@Inherited
@TestExecutionListeners(listeners={StepScopeTestExecutionListener.class,JobScopeTestExecutionListener.class},
mergeMode=MERGE_WITH_DEFAULTS)
@ExtendWith(org.springframework.test.context.junit.jupiter.SpringExtension.class)
public @interface SpringBatchTest
Annotation that can be specified on a test class that runs Spring Batch based tests.
Provides the following features over the regular Spring TestContext Framework:
- Registers a
JobLauncherTestUtilsbean named "jobLauncherTestUtils" which can be used in tests for launching jobs and steps. - Registers a
JobRepositoryTestUtilsbean named "jobRepositoryTestUtils" which can be used in tests setup to create or remove job executions. - Registers the
StepScopeTestExecutionListenerandJobScopeTestExecutionListeneras test execution listeners which are required to test step/job scoped beans.
A typical usage of this annotation with JUnit 4 is like the following:
@RunWith(SpringRunner.class)
@SpringBatchTest
@ContextConfiguration(classes = MyBatchJobConfiguration.class)
public class MyBatchJobTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private JobRepositoryTestUtils jobRepositoryTestUtils;
@Autowired
private Job jobUnderTest;
@Before
public void setup() {
this.jobRepositoryTestUtils.removeJobExecutions();
this.jobLauncherTestUtils.setJob(this.jobUnderTest); // this is optional if the job is unique
}
@Test
public void testMyJob() throws Exception {
// given
JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
// when
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);
// then
Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}
}
For JUnit 5, this annotation can be used without manually registering the
SpringExtension since @SpringBatchTest is meta-annotated with
@ExtendWith(SpringExtension.class). Here is an example:
@SpringBatchTest
@SpringJUnitConfig(MyBatchJobConfiguration.class)
public class MyBatchJobTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private JobRepositoryTestUtils jobRepositoryTestUtils;
@BeforeEach
public void setup(@Autowired Job jobUnderTest) {
this.jobLauncherTestUtils.setJob(jobUnderTest); // this is optional if the job is unique
this.jobRepositoryTestUtils.removeJobExecutions();
}
@Test
public void testMyJob() throws Exception {
// given
JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
// when
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);
// then
Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}
}
It should be noted that if the test context contains a single job bean definition, that
is the job under test, then this annotation will set that job in the
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). - Since:
- 4.1
- Author:
- Mahmoud Ben Hassine, Taeik Lim
- See Also: