java - 依赖注入(inject)在带有 Junit 5 集成测试的 SpringBoot 中不起作用

标签 java spring spring-boot junit junit5

我正在努力根据此 tutorial 设置与 SpringBoot 和 Junit5 的集成测试

但是当我在没有 @RunWith(SpringRunner.class) 注释的情况下运行这个测试文件时,因为没有注入(inject) RecordService,所以给出了 NullPointerException。

@ExtendWith(SpringExtension.class)
@SpringBootTest
@DefaultTestAnnotations // This is my meta-annotations
public class RecordServiceImplTest {

    @Autowired
    private RecordService recordService; // This is null.

    @Test
    public void whenSearchParametersAreProvided_ItShouldGetTheGoldenRecord() throws MdmMatchServiceException {
        GoldenRecordDTO searchParams = new GoldenRecordDTO();
        searchParams.setCountryCode("CN");
        searchParams.setName("neeraj");
        assertNotNull(recordService.getGoldenRecord(searchParams));
    }
}

是否必须使用 @RunWith(SpringRunner.class) 来运行集成测试?

最佳答案

我怀疑您导入了 JUnit4 注释 org.junit.Test 而不是 JUnit5 注释:org.junit.jupiter.api.Test

关于java - 依赖注入(inject)在带有 Junit 5 集成测试的 SpringBoot 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55392123/

相关文章:

java - Play 在刷新时重新编译所有文件?

java - 使用 Appium 并行运行 2 个真实的 Android 设备

java - 基于客户端 IP 的 Spring 登录异常

mysql - 当表是 "product_def"时,为什么 Spring Data/Hibernate 查询表 "productDef"?

spring - 在 Spring Security 5 OAuth 客户端和 Spring Boot 2.0 中,authorizationGrantType 不能为 null

java - 在远程调试中暂停 Spring Boot Java 应用程序时,ECS 代理停止并重新启动 Docker 容器

java - == 运算符对 String 有什么用?

Java反射,获取方法append并使用它

java - 多部分表单请求抛出 : Required MultipartFile parameter 'image' is not present

java - WebMvcTest 与真实服务实现