IDEA junit单元测试笔记
一、maven项目添加JUnit依赖配置的步骤
发现这篇博客写得很详细:
https://blog.csdn.net/yaya_jn/article/details/124568680
单元测试过程中遇到的坑
- 空指针报错1:
java.lang.NullPointerException
at com.gxlu.meta.help.MetaObjectHelper.getMetaClass(MetaObjectHelper.java:66)
at com.gxlu.meta.help.MetaObjectHelper.newInstance(MetaObjectHelper.java:168)
··· ···
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
解决方案:
在测试类上方加上@RunWith(SpringRunner.class)注解
原因:
注解的意义在于Test测试类要使用注入的类,比如@Autowired注入的类,
有了@RunWith(SpringRunner.class)这些类才能实例化到spring容器中,自动注入才能生效。
从博客https://blog.csdn.net/CPrimer0/article/details/115468334得知。
- 空指针报错2
注入bean失败。
下面这篇博客讲解很清晰
https://blog.csdn.net/mr_evanchen/article/details/80598245
@SpringBootTest注解
https://blog.csdn.net/weixin_39220472/article/details/87714756