SpringBoot Test类注入 SpringBoot Test类注入失败的解决

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot Test类注入 SpringBoot Test类注入失败的解决

背着梦的幸存者   2021-03-02 我要评论
想了解SpringBoot Test类注入失败的解决的相关内容吗,背着梦的幸存者在本文为您仔细讲解SpringBoot Test类注入的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot,Test类,注入失败,下面大家一起来学习吧。

如下所示

本来 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 {

这下能注入容器中了。

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们