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 ...
随机推荐
- jenkins报错解决
https://www.cnblogs.com/yangxia-test/p/4368778.html https://blog.csdn.net/xlyrh/article/details/5138 ...
- 【转载】execute、executeUpdate、executeQuery三者的区别(及返回值)
1. ResultSet executeQuery(String sql); 执行SQL查询,并返回ResultSet 对象. 2.int executeUpdate(String sql); 可执行 ...
- PTA --- L1-002 打印沙漏
题目地址 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号:各行符号中心对 ...
- C++返回对象和返回引用
我们发现,在C++中,有些成员函数返回的是对象,而有些函数返回的又是引用. 返回对象和返回引用的最主要的区别就是函数原型和函数头. Car run(const Car &) //返回对 ...
- 2019年最新Web安全攻防班课程
咨询请加QQ:1373721311 或 2644148223 课程核心大纲总览 Web安全攻防班以业界权威的OWASP TOP 10的漏洞为主干课程.更加详细教学思维导图可访问如下链接:https:/ ...
- redhat 5中ifconfig不能使用问题
ifconfig不能使用问题 输入 ifconfig 命令的绝对路径, ifconfig在是/sbin这个目录下面, 所以在终端输入下列命令就可以运行此命令:/sbin/ifconfig 我们还可以修 ...
- 阿里云服务器安装svn完整步骤,避免新手可能出现的所有错误
centos6.8,没有安装svn的情况: 1.安装: yum install subversion (这一步一般不会错) 2.创建svn版本库: cd /root mkdir -p svn/proj ...
- 【LOJ】#3032. 「JOISC 2019 Day1」馕
LOJ#3032. 「JOISC 2019 Day1」馕 处理出每个人把馕切成N段,每一段快乐度相同,我们选择第一个排在最前的人分给他的第一段,然后再在未选取的的人中选一个第二个排在最前的切一下,并把 ...
- centos7搭建NFS服务
服务器端 139.155.90.78 客户端 192.168.198.146 先查看自己的系统有没有安装rpcbind 和nfs-utils rpm -qa nfs-utils rpcbind 若使 ...
- Keepalive+双主
一.建立3台服务器之间ssh互信在mydb1,mydb2,mydb3服务器上分别执行:ssh-keygen -t rsassh-copy-id -i .ssh/id_rsa.pub root@192. ...