Spring批处理Java配置JobLauncherTestUtils
我目前正在使用spring批处理的spring引导项目。我正尝试使用JavaConfig而不是xml,但对于当前所有的xml文档都很困难。Spring批处理Java配置JobLauncherTestUtils
我跟着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();
}
}
你有没有在你的pom.xml以下?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
如果我没有记错的话,和你用春天开机,它应该加载弹簧批给你的自动配置豆所以他们将可用于注射。
是的,我有依赖。代码编译只是无法获得正确的上下文。 –
Spring引导使用以下模式在测试环境中查找您的配置:1)在您的测试包中搜索最近的'@ SpringBootApplication'。 2)在主包中搜索最近的'@ SpringBootApplication'。你有这些吗?如果不是,你可以用'@ ComponentScan'在你的测试平台中创建一个搜索'@ Configuration'文件的文件。 – Tom
我偶然发现了同一个问题,并且看到了Spring Batch示例中的this XML configuration。根据我设法得到它的工作:
@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
}
}
测试成功和我ItemWriter
记录该处理的元素如预期。
由于某种原因使用这种方法,我得到了“没有可用的org.springframework.batch.core.Job类型的bean”,直到我将BatchTestConfig移动到其自己的类中。春天的怪癖我从来没有遇到过或者我不知道的错误......否则+1 – dgtc
您是否曾尝试将TestExecutionListener注释添加到测试类以注入配置的应用程序上下文? '@TestExecutionListeners({的DependencyInjectionTestExecutionListener.class, })' 看一看http://docs.spring.io/spring-batch/reference/html/testing.html#testingIndividualSteps怎么看工作,如何在那里测试单个步骤。 –
@Sander_M但要做到这一点,我需要有'JobLauncherTestUtils'工作这是我的问题。我正在尝试进行端到端或个别步骤测试,而不仅仅是测试组件。 –