服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBoot Test类注入失败的解决

SpringBoot Test类注入失败的解决

2021-08-18 11:47背着梦的幸存者 Java教程

这篇文章主要介绍了SpringBoot Test类注入失败的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示

SpringBoot Test类注入失败的解决

本来 bookService的引用一直是null。

导致每次测试都报空指针异常。

然后现在继承相应的 ApplicationTests类,然后使用@Component将该类注册为组件。就可以正常注入了。

补充:关于springboot test @Mapper ,@Autiwired注入无效的问题

?
1
2
3
4
5
@SpringBootTest()
@RunWith(SpringRunner.class)
public class ProductMapperTest {
  @Autowired
  ProductMapper productMapper;

为了给mapper接口 自动根据一个添加@Mapper注解的接口生成一个实现类

怎么注入都是失败,ProductMapper 使用@Mapper 注解,这个不能注入到spring 容器中(其中原因还是不了解)。

@Autowired 注入不进去的。

?
1
2
3
@Repository
@Mapper
public interface ProductMapper {

这下能注入容器中了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/qq_33247435/article/details/99238056

延伸 · 阅读

精彩推荐