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

Dependency Injection not working in SpringBoot with Junit 5 Integration test

我正在使用 SpringBoot 和 Junit5 设置一个集成测试 tutorial

但是当我 运行 这个没有 @RunWith(SpringRunner.class) 注释的测试文件给出 NullPointerException 因为没有注入 RecordService。

@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));
    }
}

对于 运行ning 集成测试是否必须有 @RunWith(SpringRunner.class)

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