【问题标题】:Could not autowire. No beans of 'JobRepositoryTestUtils' type found. With SpringBatchTest Annotation added无法自动接线。找不到“JobRepositoryTestUtils”类型的 bean。添加了 SpringBatchTest 注解
【发布时间】:2021-08-18 09:26:25
【问题描述】:

我正在使用 @SpringBatchTest 注释进行 Spring Batch 单元测试,该注释应该自动添加 JobLauncherTestUtilsJobRepositoryTestUtils 的 bean。

这里是 Job 配置类:

@Configuration
@EnableBatchProcessing
public class JobConfiguration {

    @Bean
    public Job getJob(JobBuilderFactory jobBuilderFactory,
                      @Qualifier("flow_master") Flow flowMaster) {

        return jobBuilderFactory.get("job")
                .incrementer(new RunIdIncrementer())
                .start(flowMaster)
                .build().build();
    }
}

这是测试类:

@ExtendWith(SpringExtension.class)
@SpringBatchTest
@ContextConfiguration(classes = JobConfiguration.class)
public class SpringBatchTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Before
    public void clearJobExecutions() {
        this.jobRepositoryTestUtils.removeJobExecutions();
    }

    @Test
    public void testMyJob() throws Exception {

        JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();

        JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);

        Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
    }

}

问题:

我收到错误消息:

  • 无法自动接线。找不到“JobLauncherTestUtils”类型的 bean。

我已经克隆了一些应该可以工作的 repo 示例,但我都遇到了同样的错误。

我错过了什么吗?

【问题讨论】:

    标签: java spring unit-testing testing spring-batch


    【解决方案1】:

    您没有共享您的导入,但您可能在 junit4 和 junit5 之间混合了导入。我无法重现您的错误。这是一个完整的例子:

    作业配置类:

    package com.example.demo;
    
    import javax.sql.DataSource;
    
    import org.springframework.batch.core.Job;
    import org.springframework.batch.core.JobParameters;
    import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
    import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
    import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
    import org.springframework.batch.core.launch.JobLauncher;
    import org.springframework.batch.repeat.RepeatStatus;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
    import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
    
    @Configuration
    @EnableBatchProcessing
    public class MyJobConfig {
    
        @Bean
        public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
            return jobs.get("job")
                    .start(steps.get("step")
                            .tasklet((contribution, chunkContext) -> {
                                System.out.println("Hello world!");
                                return RepeatStatus.FINISHED;
                            }).build())
                    .build();
        }
    
        @Bean
        public DataSource dataSource() {
            return new EmbeddedDatabaseBuilder()
                    .setType(EmbeddedDatabaseType.H2)
                    .addScript("/org/springframework/batch/core/schema-h2.sql")
                    .build();
        }
    
        public static void main(String[] args) throws Exception {
            ApplicationContext context = new AnnotationConfigApplicationContext(MyJobConfig.class);
            JobLauncher jobLauncher = context.getBean(JobLauncher.class);
            Job job = context.getBean(Job.class);
            jobLauncher.run(job, new JobParameters());
        }
    
    }
    

    测试类:

    package com.example.demo;
    
    import org.junit.jupiter.api.Assertions;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    
    import org.springframework.batch.core.ExitStatus;
    import org.springframework.batch.core.JobExecution;
    import org.springframework.batch.core.JobParameters;
    import org.springframework.batch.test.JobLauncherTestUtils;
    import org.springframework.batch.test.JobRepositoryTestUtils;
    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.junit.jupiter.SpringExtension;
    
    @ExtendWith(SpringExtension.class)
    @SpringBatchTest
    @ContextConfiguration(classes = MyJobConfig.class)
    class MyJobConfigTest {
    
        @Autowired
        private JobLauncherTestUtils jobLauncherTestUtils;
    
        @Autowired
        private JobRepositoryTestUtils jobRepositoryTestUtils;
    
        @BeforeEach
        public void clearJobExecutions() {
            this.jobRepositoryTestUtils.removeJobExecutions();
        }
    
        @Test
        public void testMyJob() throws Exception {
            // given
            JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
    
            // when
            JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);
    
            // then
            Assertions.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
        }
    
    }
    

    还有pom.xml 文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>so67767525</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>so67767525</name>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.batch</groupId>
                <artifactId>spring-batch-core</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.batch</groupId>
                <artifactId>spring-batch-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
        <!-- While this has "boot" in the name, it does not bring any Spring Boot feature. -->
        <!-- It is used to manage Spring projects dependencies that are known to work correctly together -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>2.4.6</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
    </project>
    

    测试成功运行,没有你提到的错误。

    【讨论】:

    • 我试过你的例子,我也遇到了同样的问题。既然它对你有用,我知道我当地的环境有问题。在互联网上进行一些研究后,我通过将 IntelliJ 设置中的“自动装配 Bean 类”的严重性从“错误”更改为“警告”来解决问题。为什么 IntelliJ 会特别为 JobLauncherTestUtils 和 JobRepositoryTestUtils bean 显示此错误?
    • @SpringBatchTest 在测试上下文中创建并注入这两个 bean。 IntelliJ IDEA 无法对此进行检查,因此会出现错误(或警告,具体取决于您设置的级别)。您应该提到该错误来自您的 IDE,因为我知道您在运行时得到它。不过不用担心,最重要的是能够为您提供帮助。
    • 我相信我已经用一个例子回答了你的问题,所以请接受答案:stackoverflow.com/help/someone-answers。否则,让我知道要接受的答案中缺少什么。谢谢。
    • 我们可以用@SpringBootTest代替@ContextConfiguration
    猜你喜欢
    • 2021-07-02
    • 2021-01-17
    • 2019-08-17
    • 2020-08-31
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多