【发布时间】:2017-07-28 10:29:27
【问题描述】:
我目前正在开发一个使用 Spring Batch 的 Spring Boot 项目。我正在尝试使用 JavaConfig 而不是 xml,但是对于当前在 xml 中的所有文档来说,这很困难。
我关注了https://blog.codecentric.de/en/2013/06/spring-batch-2-2-javaconfig-part-5-modular-configurations,但在使用JobLauncherTestUtils 时遇到了困难。我知道我需要告诉测试使用正确的弹簧上下文,但我似乎不知道该怎么做。我收到以下错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.batch.test.JobLauncherTestUtils' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的测试如下所示:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyApplication.class, MyJobConfiguration.class})
public class RetrieveDividendsTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void testSomething() throws Exception {
jobLauncherTestUtils.launchJob();
}
}
【问题讨论】:
-
您是否尝试过向测试类添加 TestExecutionListener 注释以注入配置的应用程序上下文?
@TestExecutionListeners( {DependencyInjectionTestExecutionListener.class, })看看docs.spring.io/spring-batch/reference/html/…,看看那里是如何测试作业以及如何测试单个步骤的。 -
@Sander_M 但要做到这一点,我需要让
JobLauncherTestUtils工作,这是我的问题。我正在尝试进行端到端或单独的步骤测试,而不仅仅是测试组件。
标签: spring-boot spring-batch spring-java-config