目录

SpringBoot的Test测试Autowired为null

目录

SpringBoot的Test测试@Autowired为null

https://i-blog.csdnimg.cn/blog_migrate/d67441ab670ab4c93b273cd413233944.png

@RunWith(SpringRunner.class)
@SpringBootTest

需要导入springBoot的测试依赖

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

Springboot的@RunWith(SpringRunner.class)

注解的意义在于Test测试类要使用注入的类,比如@Autowired注入的类,

有了@RunWith(SpringRunner.class)这些类才能实例化到spring容器中,自动注入才能生效,

不然直接一个NullPointerExecption