Springboot单元测试 Could not autowire. No beans of XXX 解决方案

问题描述

应公司要求使用低版本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 {

    @Autowired
    private UserService userService;

    @Test
    public void Test(){
        userService.test();
    }
}

问题解决。

补充:SpringBoot多模块项目使用测试类

如果一个项目目录是多模块的(父工程包含了子工程),如下图:
在这里插入图片描述
你的SpringBoot主启动类在某一个工程里。那么就不能用上面的路径建测试类了。要把测试类的根目录(Test Sources Root)建在主启动类所在工程的如下图位置:
在这里插入图片描述
该类的代码同上。这样就可以在多模块项目中使用测试类了。

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
"Could not autowire. No beans of 'RepayCalcPlugin' type found."错误提示通常在Idea的Spring工程中出现。这个错误提示意味着在注入'RepayCalcPlugin'类型的bean时找不到对应的bean。虽然编译和运行程序没有问题,但这个红色的错误提示可能会让一些程序员感到不舒服。根据引用提供的信息,这个错误提示并不会对程序的功能产生影响。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示](https://download.csdn.net/download/weixin_38632624/12751965)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [关于Could not autowire问题的解决](https://blog.csdn.net/code_aliu/article/details/123299317)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Springboot单元测试 Could not autowire. No beans of XXX 解决方案](https://blog.csdn.net/weixin_44757863/article/details/115693244)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值