@Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Inherited @TestExecutionListeners(listeners={StepScopeTestExecutionListener.class,JobScopeTestExecutionListener.class}, mergeMode=MERGE_WITH_DEFAULTS) @ExtendWith(value=org.springframework.test.context.junit.jupiter.SpringExtension.class) public @interface SpringBatchTest
JobLauncherTestUtils
Bean を BatchTestContextCustomizer.JOB_LAUNCHER_TEST_UTILS_BEAN_NAME
に登録します。これは、ジョブおよびステップを起動するためのテストで使用できます。JobRepositoryTestUtils
Bean を BatchTestContextCustomizer.JOB_REPOSITORY_TEST_UTILS_BEAN_NAME
に登録します。これは、ジョブ実行を作成または削除するためのテストセットアップで使用できます。StepScopeTestExecutionListener
および JobScopeTestExecutionListener
を登録します。JUnit 4 でのこのアノテーションの一般的な使用箇所は、次のとおりです。
@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()); } }JUnit 5 の場合、
@SpringBatchTest
は @ExtendWith(SpringExtension.class)
でメタアノテーションが付けられているため、SpringExtension
を手動で登録しなくてもこのアノテーションを使用できます。@SpringBatchTest @ContextConfiguration(classes = MyBatchJobConfiguration.class) public class MyBatchJobTests { @@Autowired private JobLauncherTestUtils jobLauncherTestUtils; @@Autowired private JobRepositoryTestUtils jobRepositoryTestUtils; @BeforeEach 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 Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); } }
JobLauncherTestUtils
には Job
Bean が必要であり、JobRepositoryTestUtils
には DataSource
Bean が必要であることに注意してください。このアノテーションは JobLauncherTestUtils
と JobRepositoryTestUtils
をテストコンテキストに登録するため、テストコンテキストには Job
と DataSource
の単一のオートワイヤー候補(単一の Bean 定義または Primary
でアノテーションが付けられた定義)が含まれていると予想されます。
JobLauncherTestUtils
, JobRepositoryTestUtils
, StepScopeTestExecutionListener
, JobScopeTestExecutionListener