Gre*_*ter 7 spring-batch spring-boot spring-java-config
我目前正在开发一个使用弹簧批量的弹簧启动项目.我正在尝试使用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)}
Run Code Online (Sandbox Code Playgroud)
我的测试如下所示:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyApplication.class, MyJobConfiguration.class})
public class RetrieveDividendsTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void testSomething() throws Exception {
jobLauncherTestUtils.launchJob();
}
}
Run Code Online (Sandbox Code Playgroud)
我偶然发现了同样的问题,并从Spring Batch示例中查看了这个XML配置.基于此,我设法让它与:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { BatchTest.BatchTestConfig.class })
public class BatchTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void demo() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
Assert.assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}
@Configuration
@EnableBatchProcessing
static class BatchTestConfig {
@Bean
JobLauncherTestUtils jobLauncherTestUtils() {
return new JobLauncherTestUtils();
}
// rest omitted for brevity
}
}
Run Code Online (Sandbox Code Playgroud)
测试成功,我ItemWriter按预期记录处理过的元素.
你的 pom.xml 中有以下内容吗?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
如果我没记错的话,并且您使用 Spring Boot,它应该为您加载 Spring Batch 的自动配置 bean,以便它们可用于注入。
| 归档时间: |
|
| 查看次数: |
9695 次 |
| 最近记录: |