SpringBoot中整合Junit测试注入Bean失败的问题处理

最近看到这样一个问题;junt测试类引入bean失败导致查询数据报空指针。
博主就亲自测试了一下,成功注入。效果如下。

在这里插入图片描述

这里给大家讲一下如何处理bean注入失败的问题。

1:引入pom依赖

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

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

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>

2:创建测试类

加上下图中的两个注解
SpringbootApplication指的是启动类(非测试启动类)

在这里插入图片描述

注意的是测试包不要引用错了
在这里插入图片描述

这样就可以正常运行 了,希望能帮到大家。