大家好,我是神韵,是一个技术&生活博主。出文章目的主要是两个,一是好记忆不如烂笔头,记录总结中提高自己。二是希望我的文章可以帮到大家。欢迎来点赞打卡,你们的行动将是我无限的动力。
本篇主题是:Spring Batch 单元测试
Spring Batch 官网也介绍了很多,感兴趣可以先看一看
Spring Batch Unit Testing
下面进入本文主题
1、添加依赖
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<scope>test</scope>
<version>4.3.3</version>
</dependency>
2、测试类修饰注解
@SpringBootTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { SpringBatchApplication.class })
@ActiveProfiles(value = "test")
其中RunWith是必须的;SpringBootTest/ContextConfiguration可以二选一,也可以一起用上,前者功能比较多,后者指定了加载的配置类,我这里直接配置的是boot启动类。ActiveProfiles指定环境生效,我这里配置test配置文件。
3、需要的变量
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private JobLauncher launcher;
@Autowired
private JobRepository repository;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private ReadDB config;
@Autowired
private Step readDBStep;
@Before
public void setup() {
jobLauncherTestUtils = new JobLauncherTestUtils();
jobLauncherTestUtils.setJob(config.readDBJob(jobBuilderFactory, readDBStep));
jobLauncherTestUtils.setJobLauncher(launcher);
jobLauncherTestUtils.setJobRepository(repository);
}
主要需要借助JobLauncherTestUtils类来完成单元测试,这里所有的注入变量都是为了配置JobLauncherTestUtils,在setup方法中配置job,jobLauncher和jobRepository三个变量。其中主要解释一下job,这个是将配置类ReadDB通过Autowired方式注入,然后通过get方法获得一个Job类,最后通过setJob方式填充入JobLauncherTestUtils,其中ReadDB看下面截图
以下都是作为补充
其实很多时候也是可以在自己写的job中,将JobBuilderFactory和Step用@Autowired的方式注入,这个时候就不用跟我上面注入Job一样,对测试时就不需要@Autowired这两个参数了,如下
此时测试类将变成
4、写测试方法
配置好了JobLauncherTestUtils,接下来就是写测试Job,测试Step两部曲,其它processor,listener等这里暂时没有写入,但是测试也已经遍历了它们。
测试Job
@Test
public void testReadDBJob() throws Exception {
JobExecution result = jobLauncherTestUtils.launchJob();
Assert.assertNotNull(result);
Assert.assertEquals(BatchStatus.COMPLETED, result.getStatus());
}
测试Step
// Test step
@Test
public void testReadDBStep() {
JobExecution result = jobLauncherTestUtils.launchStep("readDBStep");
Assert.assertNotNull(result);
Assert.assertEquals(BatchStatus.COMPLETED, result.getStatus());
}
全代码
@SpringBootTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { SpringBatchApplication.class })
@ActiveProfiles(value = "test")
public class ReadDBTest {
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private JobLauncher launcher;
@Autowired
private JobRepository repository;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private ReadDB config;
@Autowired
private Step readDBStep;
public ReadDBTest() {
}
@Before
public void setup() {
jobLauncherTestUtils = new JobLauncherTestUtils();
// job 为具体job,每个不同test job都不一样
jobLauncherTestUtils.setJob(config.readDBJob(jobBuilderFactory, readDBStep));
jobLauncherTestUtils.setJobLauncher(launcher);
jobLauncherTestUtils.setJobRepository(repository);
}
// Test job
@Test
public void testReadDBJob() throws Exception {
JobExecution result = jobLauncherTestUtils.launchJob();
Assert.assertNotNull(result);
Assert.assertEquals(BatchStatus.COMPLETED, result.getStatus());
}
// Test step
@Test
public void testReadDBStep() {
// readDBStep为单元测试具体名称
JobExecution result = jobLauncherTestUtils.launchStep("readDBStep");
Assert.assertNotNull(result);
Assert.assertEquals(BatchStatus.COMPLETED, result.getStatus());
}
}
本文代码 GitHub link: shenyun499
到此一个Job测试完毕,本文结束!