アノテーションインターフェース 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 フレームワークに比べて次の機能を提供します。
  • ジョブとステップを起動するためのテストで使用できる "jobLauncherTestUtils" という名前の JobLauncherTestUtils Bean を登録します。
  • テストのセットアップでジョブ実行を作成または削除するために使用できる "jobRepositoryTestUtils" という名前の JobRepositoryTestUtils Bean を登録します。
  • ステップ / ジョブスコープの Bean をテストするために必要なテスト実行リスナーとして StepScopeTestExecutionListener および JobScopeTestExecutionListener を登録します。

JUnit 4 でのこのアノテーションの典型的な使用箇所は次のようになります。

 @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());
     }

 }
 
JUnit 5 の場合、@SpringBatchTest は @ExtendWith(SpringExtension.class) でメタアノテーションが付けられているため、手動で SpringExtension を登録しなくてもこのアノテーションを使用できます。次に例を示します。
 @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());
     }

 }
 
テストコンテキストが単一のジョブ Bean 定義、つまりテスト中のジョブを含む場合、このアノテーションはそのジョブを JobLauncherTestUtils に自動的に設定することに注意してください。このアノテーションがテストユーティリティを適切に設定するには、テストコンテキストに JobRepository および JobLauncher Bean が含まれている必要があります。前の例では、インポートされた構成クラス MyBatchJobConfiguration にそのような Bean が定義されている (または別の構成クラスからインポートされた) ことが期待されます。
導入:
4.1
作成者:
Mahmoud Ben Hassine, Taeik Lim
関連事項: