SpringBoot Test类注入失败的解决
作者:背着梦的幸存者 时间:2023-05-20 13:59:40
如下所示
本来 bookService的引用一直是null。
导致每次测试都报空指针异常。
然后现在继承相应的 ApplicationTests类,然后使用@Component将该类注册为组件。就可以正常注入了。
补充:关于springboot test @Mapper ,@Autiwired注入无效的问题
@SpringBootTest()
@RunWith(SpringRunner.class)
public class ProductMapperTest {
@Autowired
ProductMapper productMapper;
为了给mapper接口 自动根据一个添加@Mapper注解的接口生成一个实现类
怎么注入都是失败,ProductMapper 使用@Mapper 注解,这个不能注入到spring 容器中(其中原因还是不了解)。
@Autowired 注入不进去的。
@Repository
@Mapper
public interface ProductMapper {
这下能注入容器中了。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/qq_33247435/article/details/99238056
标签:SpringBoot,Test类,注入
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于C# dynamic装箱问题
2021-09-13 19:22:40
Spring Boot2.X国际化文件编写配置
2023-02-24 11:34:24
Android popupWindow弹出窗体实现方法分析
2021-08-08 08:07:42
android仿支付宝密码输入框效果
2021-08-06 12:14:01
![](https://img.aspxhome.com/file/2023/9/117069_0s.gif)
SpringBoot中默认缓存实现方案的示例代码
2023-11-24 05:50:30
![](https://img.aspxhome.com/file/2023/7/59937_0s.jpg)
Android开发-之环境的搭建(图文详解)
2022-01-12 00:54:26
![](https://img.aspxhome.com/file/2023/5/88695_0s.png)
Mybatis resultMap标签继承、复用、嵌套方式
2023-06-27 13:27:43
详解androidstudio项目上传到github方法以及步骤
2023-07-15 02:55:42
![](https://img.aspxhome.com/file/2023/0/139330_0s.png)
Java实现单例模式之饿汉式、懒汉式、枚举式
2022-12-17 17:50:13
![](https://img.aspxhome.com/file/2023/6/110656_0s.png)
IntelliJ IDEA使用git初始化仓库的使用方法
2022-05-24 12:37:26
![](https://img.aspxhome.com/file/2023/4/68534_0s.png)
基于Android本地代码生成器详解
2022-09-15 02:52:26
C# 批处理调用方法
2021-06-18 17:14:19
基于<aop:aspect>与<aop:advisor>的区别
2023-01-22 21:53:35
Intellij idea热部署插件JRebel的使用
2022-12-02 14:13:27
![](https://img.aspxhome.com/file/2023/6/131766_0s.png)
javaweb实战之商城项目开发(三)
2023-04-15 14:27:34
![](https://img.aspxhome.com/file/2023/4/97804_0s.png)
Android中如何优雅的处理重复点击实例代码
2023-09-28 12:14:35
Java中Equals使用方法汇总
2022-09-14 23:08:28
Flutter实现切换应用时隐藏应用预览
2021-07-20 21:05:46
![](https://img.aspxhome.com/file/2023/5/130325_0s.jpg)
Java DecimalFormat 保留小数位及四舍五入的陷阱介绍
2023-11-09 04:49:33
C#读写xml文件方法总结(超详细!)
2023-11-23 13:16:40