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
JobOperatorTestUtils
bean named "jobOperatorTestUtils" which can be used in tests for starting jobs and steps. - Registers a
JobRepositoryTestUtils
bean named "jobRepositoryTestUtils" which can be used in tests setup to create or remove job executions. - Registers the
StepScopeTestExecutionListener
andJobScopeTestExecutionListener
as test execution listeners which are required to test step/job scoped beans.
SpringExtension
since @SpringBatchTest
is meta-annotated with
@ExtendWith(SpringExtension.class)
. Here is an example:
@SpringBatchTest @SpringJUnitConfig(MyBatchJobConfiguration.class) public class MyBatchJobTests { @Autowired private JobOperatorTestUtils jobOperatorTestUtils; @Autowired private JobRepositoryTestUtils jobRepositoryTestUtils; @BeforeEach public void setup(@Autowired Job jobUnderTest) { this.jobOperatorTestUtils.setJob(jobUnderTest); // this is optional if the job is unique this.jobRepositoryTestUtils.removeJobExecutions(); } @Test public void testMyJob() throws Exception { // given JobParameters jobParameters = this.jobOperatorTestUtils.getUniqueJobParameters(); // when JobExecution jobExecution = this.jobOperatorTestUtils.startJob(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
JobOperatorTestUtils
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). JUnit4 support is
deprecated in Spring Batch 6.0.0 and will be removed in a future release.- Since:
- 4.1
- Author:
- Mahmoud Ben Hassine, Taeik Lim, Hyuntae Park
- See Also: