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 ...
随机推荐
- jQuery.extend函数
http://www.cnblogs.com/luckboy/archive/2009/06/25/1510870.html 1.扩展jQuery静态方法. 1$.extend({ 2test:fun ...
- eclipse中Maven web项目的目录结构浅析
刚开始接触maven web项目的时候,相信很多人都会被它的目录结构迷惑. 为了避免初学者遇到像我一样的困扰,我就从一个纯初学者的视角,来分析一下这个东西. 1,比如说,我们拿一个常见的目录结构来看, ...
- Python3 循环_break和continue语句及循环中的else子句
break和continue语句及循环中的else子句break语句可以跳出for和while的循环体.如果你从for或while循环中终止,任何对应的循环else块将不执行. continue语句被 ...
- Minimum number of swaps required to sort an array
https://www.hackerrank.com/challenges/minimum-swaps-2/problem Minimum Swaps II You are given an unor ...
- 连载一:RobotFramework+SeleniumWebdriver+RIDE的安装
安装前说明: Robot Framework自动化测试框架+可视化编辑工具RIDE+Selenium2这是规范的webAPI. 一.通过下载安装包安装 1)RF 框架是基于 Python 语言的,所以 ...
- Spring boot Gradle项目搭建
Spring boot Gradle项目搭建 使用IDEA创建Gradle工程 操作大致为:File->new->Project->Gradle(在左侧选项栏中) 创 ...
- JoinableQueue队列,线程,线程于进程的关系,使用线程,线程的特点,守护线程,线程的互斥锁,死锁问题,递归锁,信号量
1.JoinableQueue队列 JoinableQueue([maxsize]):这就像是一个Queue对象,但是队列允许项目的使用者通知生成者项目已经被成功处理.通知进程是使用共享的信号和条件变 ...
- Centos7搭建主从DNS服务器
1.准备 例:两台192.168.11.10(主),192.168.11.11(从),域名www.test1.com # 主从DNS服务器均需要安装bind.bind-chroot.bind-util ...
- 什么是MapReduce?
[学习笔记] 什么是MapReduce?马 克-to-win @ 马克java社区:1)MapReduce是面向大数据并行程序设计的模型和方法,这一点很像我们前面讲的MVC,MVC解决动态网站问题而 ...
- keepalived脑裂问题
一.对脑裂的理解 在高可用(HA)系统中,当联系2个节点的“心跳线”断开时,本来为一整体.动作协调的HA系统,就分裂成为2个独立的个体.由于相互失去了联系,都以为是对方出了故障.两个节点上的HA软件像 ...