其他乱七八糟配置就不扯了,先上项目结构图

配置好参数后我再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注入的更多相关文章

  1. 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 ...

  2. JPA实体类注解、springboot测试类、lombok的使用

    前提准备: 搭建一个springboot项目,详情请参见其它博客:点击前往 1 引入相关依赖 web.mysql.jpa.lombok <?xml version="1.0" ...

  3. IntelliJ IDEA 2017版 SpringBoot测试类编写

    SpringBoot的测试类编写Demo 源码见 https://github.com/liushaoye/baseone.git

  4. springboot测试类

    Controller测试类 /** * Created by zhiqi.shao on 2017/5/12. */ @RunWith(SpringJUnit4ClassRunner.class) @ ...

  5. springboot 测试类,项目使用shiro时报错UnavailableSecurityManagerException

    大概的问题就是,正常运行项目是没有问题的 使用测试类是,加载不了shiro的securityManager,主要导致不是很清楚,望告知, 解决方法 @Resource org.apache.shiro ...

  6. Springboot测试类之@RunWith注解

    @runWith注解作用: --@RunWith就是一个运行器 --@RunWith(JUnit4.class)就是指用JUnit4来运行 --@RunWith(SpringJUnit4ClassRu ...

  7. SpringBoot 测试类 @RunWith & @SpringBootTest

    @RunWith(SpringRunner.class) @SpringBootTest public class RabbitMqTest { @Autowired RabbitMqSender r ...

  8. 尚硅谷springboot学习9-配置文件值注入

    首先让我想到的是spring的依赖注入,这里我们可以将yaml或者properties配置文件中的值注入到java bean中 配置文件 person: lastName: hello age: 18 ...

  9. Springboot中如何在Utils类中使用@Autowired注入bean

    Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类: 1. 使用@Component注解标记工具类Statisti ...

随机推荐

  1. Selenium 2自动化测试实战39(Page Object设计模式)

    Page Object设计模式 Page object是selenium自动化测试项目开发时间的最佳设计模式之一,主要体现在对界面交互细节的封装. 1.认识page object优点如下:1.减少代码 ...

  2. hutool-all 包把实体Bean转化成字符串,以及把字符串转化成Bean对象

    GxyJobEntity gxyJobEntity1 = new GxyJobEntity(); gxyJobEntity1.setUserId("user001"); gxyJo ...

  3. CentOS7+rsync+sersync实现数据实时同步

    一.全网数据备份方案 1.需要备份的文件目录有(原则上,只要运维人员写入或更改的数据都需要备份)./data,/etc/rc.local,/var/spool/cron/root等,根据不同都服务器做 ...

  4. 数组中存放model去重

    在这个项目中出现"添加model数据"数组重复的情况,这就涉及到数组去重的问题了...... 1. 一开始使用的最笨的方法: 依次循环两个数组(原有的数组,选择的数组),双重for ...

  5. nodeslector使用

    问题: node节点挂了一个, 无法切换到另一个node上 解决: .指定了 nodeslector .设置了下面: hostNetwork: true dnsPolicy: ClusterFirst ...

  6. gitbook组织管理书写

    1.git 强大的版本管理工作,也适合对书写内容的更新管理. 2.markdown git管理文本,所以书写要从docx格式转变成markdown. 而typora是一个比较好用markdown书写工 ...

  7. JAVA是否适合非科班者自学入行?石油工程专业从培训到JAVA入门自学亲身经历

    如今的我已经过了三十而立的年纪,虽然在三十岁我没有立下任何事业,相反,还在茫茫苦海中挣扎. 但是我并不是没有收获.当然,曾经在我拥有大好青春年华的时候选择了迷茫,以至于当我有所明悟的时候,却已经错过了 ...

  8. 解决使用wamp怎么使用php命令行

    用了wamp,把php加到环境变量就报错啊! 那怎么用命令行啊! 可以找么着:(比如想执行 php think build --module demo) E:\php-project\tp5.> ...

  9. sops的配置过程

    0.demo关键 1.关键信息,不能用5.1.6版本的,还有很多坑 蓝鲸版本: 标准运维:bk_sops_V3.1.39.tar 2.简单理解: 标准运维实际上调用作业平台job的API去执行 要跑通 ...

  10. Flutter与Xamarin跨平台移动开发相比

    在过去十年中,移动行业经历了巨大的增长,特别是在应用程序开发方面.据Statista报告称,全球智能手机用户超过20亿,预计到2022年底这一数字将增加到50亿以上.在这些智能手机中,近100%在三个 ...