依赖注入在使用Junit 5 Integration测试的SpringBoot中不起作用

时间:2019-03-28 07:22:33

标签: java spring spring-boot junit junit5

根据此tutorial

,我正在与SpringBoot和Junit5建立集成测试。

但是当我运行没有@RunWith(SpringRunner.class)注释的测试文件时,由于没有注入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)来运行集成测试吗?

1 个答案:

答案 0 :(得分:2)

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

相关问题