【发布时间】:2020-01-14 02:37:15
【问题描述】:
我正在尝试测试一个简单的 spring 批处理应用程序。
使用 Spring Batch 文档作为指南 (found here),我创建了以下测试类:
import org.junit.runner.RunWith;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = BatchConfig.class)
class BatchConfigTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void userStep() {
assertNotNull(jobLauncherTestUtils, "jobLauncherTestUtils should not be null");
}
}
根据文档@SpringBatchTest 应该注入JobLaucherTestUtils bean。但是,当我运行测试时,断言失败。我也尝试在内部配置类中定义 bean 并得到相同的结果:
static class TestConfiguration {
@Autowired
@Qualifier("userJob")
private Job userJob;
@Bean
public JobLauncherTestUtils jobLauncherTestUtils() {
JobLauncherTestUtils utils = new JobLauncherTestUtils();
utils.setJob(userJob);
return utils;
}
}
我有什么遗漏吗?完整的源代码可以在here找到。
【问题讨论】:
-
您能指定您使用的是哪个版本的 Spring Batch 和 Junit 吗?
-
@MahmoudBenHassine 我正在使用 Spring Batch v4.2.0 和 JUnit 5
-
好的,谢谢。让我检查一下并尽快回复您。
标签: spring junit spring-batch