介绍
这篇文章将为大家详细讲解有关SpringBoot测试类注入失败怎么办,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
如下所示
本来bookService的引用一直是null。
导致每次测试都报空指针异常。
然后现在继承相应的ApplicationTests类,然后使用@ component将该类注册为组件。就可以正常注入了。
<强>补充:关于SpringBoot测试@Mapper @Autiwired注入无效的问题强>
@SpringBootTest () @RunWith (SpringRunner.class) public class  ProductMapperTest { @ autowired才能 ProductMapper 才能;productMapper;
为了给mapper接口自动根据一个添加@Mapper注解的接口生成一个实现类
怎么注入都是失败,productMapper使用@Mapper注解,这个不能注入到春容器中(其中原因还是不了解)。
@ autowired注入不进去的。
@ @Mapper public interface  ProductMapper {
这下能注入容器中了。