我正在努力根据此 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/