Springboot单元测试 Could not autowire. No beans of XXX 解决方案
- 问题描述
- 原因分析
- 解决方案
- 1、首先检查你的测试类路径有没有写对
- 2、在测试类上添加注解:
- 补充:SpringBoot多模块项目使用测试类
问题描述
应公司要求使用低版本Springboot(2.0.1.RELEASE),启动测试类SpringBootTest时无法依赖注入,使用@Autowired注解会报红,一旦强行运行测试类,就会报空指针(java.lang.NullPointerException)异常:
原因分析
猜测是SpringBoot版本问题,因为在高版本上不用这么麻烦,直接用@SpringBootTest一个注解即可搞定。
解决方案
1、首先检查你的测试类路径有没有写对
2、在测试类上添加注解:
@SpringBootTest(classes=ApiApplication.class)
@RunWith(SpringRunner.class)
第一行,classes就填写你SpringBoot主启动类的Class;
第二行,@RunWith(SpringRunner.class)
写死。引入的包是:import org.junit.runner.RunWith;
完整代码如下:
package cn.xxwlh.api;import cn.xxwlh.api.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@SpringBootTest(classes=ApiApplication.class)
@RunWith(SpringRunner.class)
public class ApiApplicationTest {@Autowiredprivate UserService userService;@Testpublic void Test(){userService.test();}
}
问题解决。
补充:SpringBoot多模块项目使用测试类
如果一个项目目录是多模块的(父工程包含了子工程),如下图:
你的SpringBoot主启动类在某一个工程里。那么就不能用上面的路径建测试类了。要把测试类的根目录(Test Sources Root)建在主启动类所在工程的如下图位置:
该类的代码同上。这样就可以在多模块项目中使用测试类了。
Springboot单元测试 Could not autowire. No beans of XXX 解决方案相关推荐
- Could not autowire. No beans of xxx 解决办法
我们遇到到类似爆红的情况,但是不影响运行,但是有强迫症的就很难受了,可以通过以下方式解决. 方法一:在mapper类中加入@Repository注解 方法二:将@Autowired 改成@Resour ...
- SpringBoot报错:Could not autowire. No beans of ‘DiscussantMapper‘ type found
SpringBoot报错:Could not autowire. No beans of 'DiscussantMapper' type found 一.资源 @Mapper和@Repository注 ...
- springboot单元测试类
springboot单元测试类,引入下方jia包 <!--version 依据当前springboot版本即可--> <dependency><groupId>or ...
- Could not autowire. No beans of ‘UserMapper‘ type found.
报红内容: 在这里可以发现userMapper处爆红,显示Could not autowire. No beans of 'UserMapper' type found.,但是在程序运行过程中并未影响 ...
- SpringBoot单元测试RunWith注解无法解析
关于SpringBoot单元测试RunWith注解没有提示,无法解析,没有导包提示的问题,笔者在此说明一下. 首先说明一下项目的环境 IDE是IDEA 创建项目使用的是Spring Initializ ...
- SpringBoot --- 单元测试(2.1.x及其它版本)
SpringBoot --- 单元测试(2.1.x及其它版本) 1.SrpingBoot 2.1.x 1.1 依赖 1.2 测试 2.SpringBoot 2.2.x及以上 2.1 依赖 2.2 测试 ...
- idea中Could not autowire. No beans of 'xxxxMapper' type found.问题解决
idea中Could not autowire. No beans of 'xxxxMapper' type found.问题解决 解决方案: 建议给Mapper增加@Repository注解,并加入 ...
- mockito mock void方法_用过举手!SpringBoot 单元测试利器Mockito
Mockito 是一种 Java mock 框架,他主要是用来做 mock 测试的,他可以模拟任何 Spring 管理的 bean.模拟方法的返回值.模拟抛出异常...等,在了解 Mockito 的具 ...
- Could not autowire. No beans of 'JavaMailSender' type found..md
Could not autowire. No beans of 'JavaMailSender' type found. 导入依赖 <dependency><groupId>o ...
最新文章
- Debian/Ubuntu/Centos下编译安装RocksDB
- 【Python教程】七种创建对象的方式,你知道几种?
- Python基础教程:正则表达式re高级用法与案例
- C++继承中的同名成员变量处理方法
- Java_方法的调用①及案例
- android google map 标记,android google map添加标记和TipView
- mvc6 mysql_MVC+EF6使用MySQL+CodeFirst的详细配置
- tekla钢结构算量_BIM钢结构造价怎么做?BIM如何用于钢结构工程量计算?
- 西南科技大学OJ题 折半查找的实现1010
- c# 使用NOPI 操作Excel
- Android网络框架情景分析之NetworkManagementService和Netd交互深入分析二
- RabbitMQ — RabbitMQ使用以及原理解析
- python爬取微博评论_python爬虫抓取新浪微博数据
- 关于mingw32-make的错误
- kafka源码愫读(5)、ReplicaManager模块源码分析
- 两轴插补速度怎么给定_如何正确计算及设置DMC1000插补的矢量速度
- 人性21——离奇自杀案
- Vue中slot与slot-scope的理解及使用
- 韩国官员提议加强对加密货币监管的全球合作
- Java项目不挂断运行,运行jar包