【问题标题】:Spring Batch Java Config JobLauncherTestUtilsSpring Batch Java Config JobLauncherTestUtils
【发布时间】: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


【解决方案1】:

你的 pom.xml 中有以下内容吗?

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-batch</artifactId>
</dependency>

如果我没记错的话,你使用的是 spring boot,它应该为你加载 spring batch 的自动配置 bean,以便它们可以被注入。

【讨论】:

  • 是的,我有依赖。代码编译无法正确获取上下文。
  • Spring boot 使用以下模式在测试环境中查找您的配置:1) 在您的测试包中搜索最接近的@SpringBootApplication。 2)在你的主包中搜索最近的@SpringBootApplication。你有这些吗?如果没有,您可以在您的测试包中创建一个带有@ComponentScan 的测试包,用于搜索您的@Configuration 文件。
【解决方案2】:

我偶然发现了同样的问题,并查看了 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 按预期记录处理的元素。

【讨论】:

  • 由于某种原因,在我将 BatchTestConfig 移动/升级到它自己的类之前,我得到“没有可用的 'org.springframework.batch.core.Job' 类型的合格 bean”。我从未遇到过的 Spring 怪癖或我不知道的错误...否则 +1
  • 我有同样的错误,即使在将 BatchTestConfig 移动到它自己的类之后。获取Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.batch.core.Job' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
  • @NicolasWidart // rest omitted for brevity 部分包含一个 Job bean 的定义:@Bean public Job myJob(JobCompletionNotificationListener listener) { ... }。在我看来,您没有正确配置 Spring Batch 管道。
  • 哦,我明白了,但是我将在其配置文件中复制已完成的作业配置?
  • @NicolasWidart 您可以在测试上下文中加载非测试配置并从那里使用配置的 bean 吗?请注意特定类型的重复 bean(即,您确实需要两次,因为您可能需要更改测试环境的某些配置步骤)。
【解决方案3】:

对于 spring Batch 4.1.x 或以上版本我们可以使用 @SpringBatchTest 自动注入 jobLauncherTestUtils 的注解,查看示例以获取更多详细信息 Here

如果您无法升级到 4.1.x 或更高版本,您可以这样创建

@Bean
     public JobLauncherTestUtils getJobLauncherTestUtils(){

            return new JobLauncherTestUtils() {
                @Override
                @Autowired
                public void setJob(@Qualifier("myjobname") Job job) {
                    super.setJob(job);
                }
            };
        }  

【讨论】:

    猜你喜欢
    • 2020-07-17
    • 2015-12-10
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2019-10-03
    相关资源
    最近更新 更多