BUG:编写springboot单元测试,自动注入实体类报空指针异常

原因:修饰测试方法的Test注解导入错误

造成错误的原因是

java 复制代码
import org.junit.Test;

正确的应该是

java 复制代码
import org.junit.jupiter.api.Test

前者是Junit4,后者是Junit5

junit4的使用似乎要在测试类除了添加@SpringbootTest还要添加@RunWith(SpringRunner.class)

同时要注意spring-boot-starter-test2.2.x依赖下之前是junit4,之后是junit5

拓展:
junit4与junit5的使用

相关推荐
计算机毕设定制辅导-无忧学长2 小时前
《基于微信小程序健康习惯激励系统设计与实现》
java·spring boot·mysql·微信小程序·uniapp·健康习惯激励系统
vipxieliang3 小时前
ValidX 正则表达式预编译优化:Pattern 复用策略
java·spring boot·后端
余槐i5 小时前
Spring Boot 3.2 偶发500无日志:线程池复用导致请求上下文丢失
spring boot·redis·后端·mysql·性能优化
杨凯凡5 小时前
Spring Boot 的 @Transactional 为什么会失效?从代理、自调用到异常回滚的排查实战
java·spring boot·spring·事务·后端开发
灼行16 小时前
Autosar SecOC 需求场景BUG(需求不一致性)【草稿】
bug
云创智城-yuncitys18 小时前
YunParking 4.0:JDK 17 与 Spring Boot 3.2 的停车云升级
java·开发语言·spring boot
paopaokaka_luck20 小时前
灾害应急物资管理系统(AI应急助手、Echarts可视化数据看板、捐赠入库联动、 物资全流程管理、库存变动追溯、灾情资讯与互动反馈、物资发放公示)
java·前端·javascript·spring boot·mysql·tomcat·echarts
学长毕设1 天前
基于SpringBoot的网上流浪狗救助捐赠平台-附源码
java·spring boot·后端
码农的日常1 天前
前后端Long精度丢失的踩坑
spring boot·架构
杨运交1 天前
086-对象存储模块-基于Spring Boot的文件存储模块监控设计与实现
java·spring boot·后端