解决SpringBoot 测试类无法自动注入@Autowired的问题
作者:jingxian 时间:2023-11-02 04:34:14
原来的测试类的注解:
@RunWith(SpringRunner.class)
@SpringBootTest
一直没法自动注入,后来在@SpringBootTest,
加入启动类Application后就可以了
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
补充:spring boot项目单元测试时,@Autowired无法注入Service解决方式
首先确认:
测试类所在包名要和启动类一致
测试类注解正确
@SpringBootTest(classes = {BiDataTaskApplication.class})
@RunWith(SpringRunner.class)
费了半天劲,才找到原因,idea自动创建的测试类 @Test注解,使用的是 import org.junit.jupiter.api.Test;
应改成:
import org.junit.Test;
idea为什么这样,还不确定,有知道的大佬麻烦回复下!
正确配置截图:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/howard789/article/details/81020233
标签:SpringBoot,测试类,@Autowired
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring框架学习常用注解汇总
2023-11-10 17:38:53
Android Bluetooth蓝牙技术使用流程详解
2022-07-07 02:41:16
Java 深入浅出掌握Collection单列集合Set
2023-10-07 18:49:32
![](https://img.aspxhome.com/file/2023/5/58545_0s.gif)
java使用poi生成excel的步骤
2021-08-25 07:03:45
mybatis打印SQL,并显示参数的实例
2022-08-03 16:46:59
Kotlin协程之Flow基础原理示例解析
2021-10-17 21:07:44
![](https://img.aspxhome.com/file/2023/4/116034_0s.png)
mybatis-plus QueryWrapper自定义查询条件的实现
2023-04-05 20:50:07
Android实现简易计算功能
2022-01-10 20:42:59
![](https://img.aspxhome.com/file/2023/1/138891_0s.jpg)
Spring的AOP极简入门
2023-07-10 22:24:32
![](https://img.aspxhome.com/file/2023/2/76482_0s.png)
Android编程实现activity dialog透明背景的方法
2023-10-24 04:16:45
![](https://img.aspxhome.com/file/2023/2/139012_0s.png)
Android 获取手机信息实例详解
2021-08-21 00:22:34
![](https://img.aspxhome.com/file/2023/8/137128_0s.png)
Java计算器核心算法代码实现
2022-03-18 05:38:55
Java中instanceof关键字的用法总结
2022-12-11 15:52:27
SpringBoot实现接口参数加密解密的示例代码
2021-07-17 03:39:18
J2SE基础之命令行中编写第一个 Hello World
2023-10-05 09:43:55
![](https://img.aspxhome.com/file/2023/1/95941_0s.png)
springboot读取自定义配置文件时出现乱码解决方案
2022-01-29 11:09:40
![](https://img.aspxhome.com/file/2023/2/90032_0s.png)
IDEA自定义常用代码块及自定义快捷摸板
2022-01-13 18:54:22
![](https://img.aspxhome.com/file/2023/6/77996_0s.png)
Java中的MapStruct用法详解
2022-10-05 12:18:13
![](https://img.aspxhome.com/file/2023/4/78004_0s.png)
C#动态创建button的方法
2023-02-11 15:58:26
Spring Mvc中传递参数方法之url/requestMapping详解
2021-11-03 05:48:46