Spring Batch @SpringBatchTest 注解

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

@RunWith(SpringRunner.class)

@SpringBatchTest

@ContextConfiguration(classes = {JobConfiguration.class})

public class JobTest {

   @Autowired

   private JobLauncherTestUtils jobLauncherTestUtils;

   @Autowired

   private JobRepositoryTestUtils jobRepositoryTestUtils;

扫描二维码关注公众号,回复: 4984560 查看本文章

   @Before

   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

猜你喜欢

转载自blog.csdn.net/huyuchengus/article/details/86550742