使用Junit Test时,@Autowired 注入出现空指针异常NullPointerException问题

1,262 阅读1分钟

1.添加maven依赖

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test</artifactId>
    <scope>test</scope>
</dependency>

2.测试类添加注解

@RunWith(SpringRunner.class)
@SpringBootTest

在这里插入图片描述

3.完成~~