无法自动布线JobLauncherTestUtils [英] Could not autowire JobLauncherTestUtils

查看:31
本文介绍了无法自动布线JobLauncherTestUtils的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试一个简单的Spring Batch应用程序。

以Spring Batch文档为指导(found here),我创建了以下测试类:

import org.junit.runner.RunWith;
import org.springframework.batch.test.JobLauncherTestUtils;
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.junit4.SpringRunner;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = BatchConfig.class)
class BatchConfigTest {
    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    void userStep() {
        assertNotNull(jobLauncherTestUtils, "jobLauncherTestUtils should not be null");
    }
}

根据文档@SpringBatchTest应该注入JobLaucherTestUtilsBean。但是,当我运行测试时,断言失败。我还尝试在内部配置类中定义Bean,结果相同:

    static class TestConfiguration {
        @Autowired
        @Qualifier("userJob")
        private Job userJob;

        @Bean
        public JobLauncherTestUtils jobLauncherTestUtils() {
            JobLauncherTestUtils utils = new JobLauncherTestUtils();
            utils.setJob(userJob);
            return utils;
        }
    }

我是不是遗漏了什么?完整的源代码可以在here中找到。

推荐答案

我使用的是Spring Batch v4.2.0和JUnit 5

您正在使用@RunWith(SpringRunner.class),它用于JUnit 4。您需要使用@ExtendWith(SpringExtension.class)进行JUnit 5测试:

@SpringBatchTest
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = BatchConfig.class)
class BatchConfigTest {
   // ...
}

这篇关于无法自动布线JobLauncherTestUtils的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆