bug:Junit5报错,@SpringBootTest没有运行

  • 1、首先解决Junit5报错

java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory

添加依赖

sh 复制代码
implementation 'org.junit.platform:junit-platform-launcher:1.8.2'

java.lang.IllegalArgumentException: Error: test loader org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader not found:

这个报错也可以添加依赖解决,但添加依赖后,就算Junit5再次运行通过,你会发现@Autowired自动装载的对象为null,调用对象会报NullPointerException空指针异常。其实就是@SpringBootTest没有运行(下面解答)。

重现错误,你可以添加如下依赖(如果不考虑重现错误,可以跳过此步,此依赖不是必需添加):

sh 复制代码
implementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'

在测试代码中添加如下,查看springboot是否启动

java 复制代码
	@Autowired
	private ApplicationContext applicationContext;
	
	...
	System.out.println(applicationContext);
	...

如果junit-jupiter-engine的依赖版本过低,还会报另一种错:

org/junit/jupiter/api/extension/ScriptEvaluationException

  • 2、@SpringBootTest没有运行
    导致@SpringBootTest没有运行的原因是测试类没有放在规定路径下。当我们一开始创建springboot工程时就会自动创建一个默认的测试类,规定路径在src/test/java下。我们测试类放在这个测试类同级的包下时,再run with-》Junit Test。我们会发现SpringBoot打印了很多后台信息,恭喜你成功了。到此,@Autowired的对象也装载进来了。
相关推荐
Rain的Java大神之路6 小时前
如何快速上传10G文件
java·spring boot·redis·后端·mysql·spring cloud·面试
+VX:Fegn08959 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
需要8269 小时前
MySQL 索引 B+ 树与“查询为什么变慢了
java·spring boot·spring·spring cloud·tomcat
一 乐9 小时前
二手交易平台|基于springboot + vue二手交易平台(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序
vx-程序开发10 小时前
【计算机毕设】django校园跑腿服务系统83141
java·数据库·vue.js·spring boot·spring·elasticsearch·django
一 乐11 小时前
养老院管理系统|基于springboot + vue养老院管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·毕业设计
今年下半年14 小时前
SpringBoot整合金蝶中间件
spring boot·中间件·maven
字节探索14 小时前
别再只会写 Controller 了!一文吃透 Spring Boot 高级特性与项目实战
spring boot·spring
MetaLite15 小时前
全网最好的SpringBoot接口请求对象设计
java·spring boot·后端
摇滚侠16 小时前
《Spring Boot 3:高级与架构设计》第 2 章 IOC容器的高级机制 BeanFactoryPostProcessor 个人理解 6
java·spring boot·笔记·后端