springboot测试类@Autowired注入报错:java.lang.NullPointerException

  • Post author:
  • Post category:java


现象:

springboot配置mybatis-plus后,使用junit测试类测试,注入mapper时报错java.lang.NullPointerException。

解决:

在测试类@SpringBootTest上面添加注解:

@RunWith(SpringRunner.class)

原因:

@RunWith(SpringRunner.class)此注解的作用是让测试类在spring环境中运行,让自动注入生效,也就是让注解@Autowired起作用。



版权声明:本文为weixin_48467764原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。