Spring Batch @SpringBatchTest 注解

Spring Batch 提供了一些很是有用的工具类(例如 JobLauncherTestUtils 和 JobRepositoryTestUtils)和测试执行监听器(StepScopeTestExecutionListener 和 JobScopeTestExecutionListener)来测试批量组件。然而, 为了可以使用这些工具类,你必须明确的对它们进行配置。这个发布介绍了一个新的注解,这个注解被命名为 @SpringBatchTest 可以自动的添加工具 bean(utility beans)和监听器(listeners)来测试上下文而且为自动写入来标记为可用,下面是一个示例代码:工具

@RunWith(SpringRunner.class)测试

@SpringBatchTestspa

@ContextConfiguration(classes = {JobConfiguration.class})code

public class JobTest {ci

 

   @Autowiredrem

   private JobLauncherTestUtils jobLauncherTestUtils;get

 

   @Autowiredit

   private JobRepositoryTestUtils jobRepositoryTestUtils;io

 

 

   @Beforetable

   public void clearMetadata() {

      jobRepositoryTestUtils.removeJobExecutions();

   }

 

   @Test

   public void testJob() throws Exception {

      // given

      JobParameters jobParameters =

            jobLauncherTestUtils.getUniqueJobParameters();

 

      // when

      JobExecution jobExecution =

            jobLauncherTestUtils.launchJob(jobParameters);

 

      // then

      Assert.assertEquals(ExitStatus.COMPLETED,

                          jobExecution.getExitStatus());

   }

 

}

有关这个新注解的更多细节,请参考 Unit Testing 章节中的内容。

 

https://www.cwiki.us/display/SpringBatchZH/@SpringBatchTest+Annotation

相关文章
相关标签/搜索