Junit测试出现异常:java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils

1、背景:

Junit测试出现异常:Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.

2、现象: 

Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
    at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:30)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Process finished with exit code 1
Empty test suite.


3、原因:

不能同时用Junit4和Junit5,junit-jupiter-api在Junit5中使用
4、解决办法:

1)、排除掉 junit-jupiter-api 依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
	<exclusions>
		<exclusion>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
		</exclusion>
	</exclusions>
</dependency>

2)、官方建议如果跑Junit5建议使用IDEA 2017.3之后的版本.因为较之前的版本还不支持Junit5, 查看自己本地的IDEA为2017.1的版本, 后升级2017.3.5版本后, 在执行Junit5后问题解决.

参考资料:  Junit5官方guide https://junit.org/junit5/docs/current/user-guide/#running-tests-ide

### 解决 `java.lang.ClassNotFoundException: org.junit.platform.launcher.TestIdentifier` 的方法 当遇到 `ClassNotFoundException` 错误提示缺少 `org.junit.platform.launcher.TestIdentifier` 类时,通常是因为项目中缺失必要的 JUnit 平台启动器依赖项。 为了确保 JUnit 测试能够正常运行,在项目的构建文件(如 Maven 或 Gradle 文件)中应正确引入所需的依赖库。对于 Maven 构建工具而言,需确认 pom.xml 文件内已声明如下依赖: ```xml <dependencies> <!-- JUnit Platform Launcher --> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.9.0</version> <!-- 版本号依据实际需求调整 --> <scope>test</scope> </dependency> <!-- JUnit Jupiter API 和 Engine (如果使用 JUnit 5)--> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.9.0</version> <!-- 版本号依据实际需求调整 --> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.9.0</version> <!-- 版本号依据实际需求调整 --> <scope>test</scope> </dependency> </dependencies> ``` 上述 XML 配置片段展示了如何通过添加适当版本的 JUnit 平台启动器以及 JUnit Jupiter 组件来修复 ClassNotFoundException 异常[^1]。 此外,还需注意保持所使用的各个组件之间的兼容性,建议统一升级至最新稳定版以减少潜在冲突的可能性。完成这些更改之后重新编译并执行测试案例即可验证问题是否得到妥善处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值