@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Inherited
@TestExecutionListeners(listeners={StepScopeTestExecutionListener.class,JobScopeTestExecutionListener.class},
mergeMode=MERGE_WITH_DEFAULTS)
public @interface SpringBatchTest
JobLauncherTestUtils bean with the
BatchTestContextCustomizer.JOB_LAUNCHER_TEST_UTILS_BEAN_NAME which can be used
in tests for launching jobs and steps.
JobRepositoryTestUtils bean
with the BatchTestContextCustomizer.JOB_REPOSITORY_TEST_UTILS_BEAN_NAME
which can be used in tests setup to create or remove job executions.
StepScopeTestExecutionListener and JobScopeTestExecutionListener
as test execution listeners which are required to test step/job scoped beans.
A typical usage of this annotation is like:
@RunWith(SpringRunner.class)
@SpringBatchTest
@ContextConfiguration(classes = MyBatchJobConfiguration.class)
public class MyBatchJobTests {
@@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@@Autowired
private JobRepositoryTestUtils jobRepositoryTestUtils;
@Before
public void clearJobExecutions() {
this.jobRepositoryTestUtils.removeJobExecutions();
}
@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());
}
}
JobLauncherTestUtils,
JobRepositoryTestUtils,
StepScopeTestExecutionListener,
JobScopeTestExecutionListener