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的对象也装载进来了。
相关推荐
jbtianci1 小时前
Spring Boot管理用户数据
java·spring boot·后端
编程彩机1 小时前
互联网大厂Java面试:从Jakarta EE到微服务架构的技术场景深度解读
spring boot·分布式事务·微服务架构·java面试·jakarta ee
biyezuopinvip1 小时前
基于Spring Boot的企业网盘的设计与实现(毕业论文)
java·spring boot·vue·毕业设计·论文·毕业论文·企业网盘的设计与实现
szhf782 小时前
SpringBoot Test详解
spring boot·后端·log4j
无尽的沉默2 小时前
SpringBoot整合Redis
spring boot·redis·后端
Coder_Boy_4 小时前
技术发展的核心规律是「加法打底,减法优化,重构平衡」
人工智能·spring boot·spring·重构
猫头虎11 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
MZ_ZXD00113 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
invicinble14 小时前
springboot的核心实现机制原理
java·spring boot·后端