Springboot 测试类没有找到bean注入
其他乱七八糟配置就不扯了,先上项目结构图

配置好参数后我再src/test/java类测试访问数据库时发现bean没有正确的注入。值得注意的是,这个项目的启动类是叫App.java
所以我们必须在这个测试类上面加上注解:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
注意:SpringBoot(classes = App.class) classes后面跟的是启动类的class,千万不要随便抄网上的配置,写一些Application.class之类的,这种Application之类的类名和一些官方包里的类名一样,容易引入错误的包。
刚开始发现这个问题疯狂去网上看别人的配置文件是怎么写的,试了一天都没用,后来静下心来,把错误信息copy出来文本里仔细看
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'com.springboot.LibrarySystem.mapper.UserMapperTest':
Unsatisfied dependency expressed through field 'userMapper';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.sb.LibrarySystem.mapper.UserMapper'
available: expected at least bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
还是从这个Test类下手
本来我的类是这样的:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class UserMapperTest {
}
修改后就是这样,和我的启动类的类名是一致的:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class UserMapperTest {
完美解决!
如果百度的时候,发现查看的问题越来越深,越来越偏离最开始的问题,那十有八九是方向偏了,重新整理一下,重新开始吧
Springboot 测试类没有找到bean注入的更多相关文章
- SpringBoot测试类启动错误 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
报错 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Cont ...
- JPA实体类注解、springboot测试类、lombok的使用
前提准备: 搭建一个springboot项目,详情请参见其它博客:点击前往 1 引入相关依赖 web.mysql.jpa.lombok <?xml version="1.0" ...
- IntelliJ IDEA 2017版 SpringBoot测试类编写
SpringBoot的测试类编写Demo 源码见 https://github.com/liushaoye/baseone.git
- springboot测试类
Controller测试类 /** * Created by zhiqi.shao on 2017/5/12. */ @RunWith(SpringJUnit4ClassRunner.class) @ ...
- springboot 测试类,项目使用shiro时报错UnavailableSecurityManagerException
大概的问题就是,正常运行项目是没有问题的 使用测试类是,加载不了shiro的securityManager,主要导致不是很清楚,望告知, 解决方法 @Resource org.apache.shiro ...
- Springboot测试类之@RunWith注解
@runWith注解作用: --@RunWith就是一个运行器 --@RunWith(JUnit4.class)就是指用JUnit4来运行 --@RunWith(SpringJUnit4ClassRu ...
- SpringBoot 测试类 @RunWith & @SpringBootTest
@RunWith(SpringRunner.class) @SpringBootTest public class RabbitMqTest { @Autowired RabbitMqSender r ...
- 尚硅谷springboot学习9-配置文件值注入
首先让我想到的是spring的依赖注入,这里我们可以将yaml或者properties配置文件中的值注入到java bean中 配置文件 person: lastName: hello age: 18 ...
- Springboot中如何在Utils类中使用@Autowired注入bean
Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类: 1. 使用@Component注解标记工具类Statisti ...
随机推荐
- 数学建模python matlab 编程(椭圆声学原理画图证明,解析几何)
证明,在椭圆形的音乐厅内,从一个椭圆的一个焦点发出声音,则另一个焦点听到的声音是最大的. 分析:证明,从椭圆的一个焦点任意发射的直线经过反射后,并经过另一个焦点. 画图,过一个焦 ...
- python中计时模块timeit的使用方法
timeit 模块: timeit 模块定义了接受两个参数的 Timer 类.两个参数都是字符串. 第一个参数是你要计时的语句或者函数. 传递给 Timer 的第二个参数是为第一个参数语句构建环境的导 ...
- EM算法:入门案例
概率分布 4种实验结果 \(E_1\) \(E_2\) \(E_3\) \(E_4\) 记录它们发生的次数 \(y_1\) \(y_2\) \(y_3\) \(y_4\) 记录次数结果 125 18 ...
- Python input 和 raw_input的区别
转载[http://www.pythonclub.org/python-basic/input] 使用input 和 raw_input 都可以读取控制台的输入,但是input和raw_input在处 ...
- maria 忘记密码
1.找到mariadb配置文件命令:find / -name my.cnf 备注:一般是在 /etc/my.cnf 2.修改配置文件 在MariaDB配置文件/etc/my.cnf的[mysqld]配 ...
- VMware 虚拟机安装Mac OS X 10.10
有图有真相,哈哈 一.下载以上文件 1. vm百度软件下载即可,版本都能满足需要,随意好了 2. unlocker 207 3. Mac OS X 10.10镜像 二.基本步骤 1. 虚拟机的安装 下 ...
- mac编译openJDK8
买了<深入理解Java虚拟机> 周志明著这本书一直没有看,我们的程序都用JVM在跑,所以学习java必须掌握JVM,才能理解我们对象到底干嘛了. 1.为什么要编译JDK 想要一探JDK ...
- Centos 7 忘记密码的情况下,修改root密码
应用场景 linux管理员忘记root密码,需要进行找回操作. 注意事项:本文基于centos7.4环境进行操作,由于centos的版本是有差异的,继续之前请确定好版本 操作步骤 一.重启系统,在开机 ...
- VirtualBox本地虚拟机常见问题
SSH连接本地虚拟机配置 https://www.jianshu.com/p/d59ed9f226d1 开启双向复制https://blog.csdn.net/wcx1293296315/articl ...
- sql 循环转移备份数据
--创建表结构 select top 1 * into ATable_20190710 from ATable --转移表数据 insert into ATable_20190710 select t ...