アノテーションインターフェース SpringBatchTest
@TargetSE(TYPESE)
@RetentionSE(RUNTIMESE)
@DocumentedSE
@InheritedSE
@TestExecutionListeners(listeners={StepScopeTestExecutionListener.class,JobScopeTestExecutionListener.class},
mergeMode=MERGE_WITH_DEFAULTS)
@ExtendWith(org.springframework.test.context.junit.jupiter.SpringExtension.class)
public @interface SpringBatchTest
Spring Batch ベースのテストを実行するテストクラスで指定できるアノテーション。通常の Spring TestContext フレームワークに比べて次の機能を提供します。
- ジョブおよびステップを開始するためのテストで使用できる "jobOperatorTestUtils" という名前の
JobOperatorTestUtilsBean を登録します。 - テストのセットアップでジョブ実行を作成または削除するために使用できる "jobRepositoryTestUtils" という名前の
JobRepositoryTestUtilsBean を登録します。 - ステップ / ジョブスコープの Bean をテストするために必要なテスト実行リスナーとして
StepScopeTestExecutionListenerおよびJobScopeTestExecutionListenerを登録します。
@SpringBatchTest は @ExtendWith(SpringExtension.class) でメタアノテーションされているため、このアノテーションは SpringExtension を手動で登録することなく使用できます。以下に例を示します。
@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());
}
}
テストコンテキストに単一のジョブ Bean 定義 (つまりテスト対象のジョブ) が含まれている場合、このアノテーションによってそのジョブが JobOperatorTestUtils に自動的に設定されることに注意してください。このアノテーションがテストユーティリティを適切に設定するには、テストコンテキストに JobRepository および JobLauncher Bean が含まれている必要があります。前の例では、インポートされた構成クラス MyBatchJobConfiguration にそのような Bean が定義されている (または別の構成クラスからインポートされた) ことが期待されます。、 JUnit4 のサポートは Spring Batch 6.0.0 では非推奨であり、将来のリリースでは削除される予定です。。- 導入:
- 4.1
- 作成者:
- Mahmoud Ben Hassine, Taeik Lim, Hyuntae Park
- 関連事項: