2.2.6.RELEASE版本的springboot 单元测试报错 java.lang.NoClassDefFoundError: org/junit/platform/engine/support/

本文介绍了在升级SpringBoot项目到2.2.6版本并尝试使用JUnit5进行单元测试时遇到的NoClassDefFoundError,重点讲述了如何处理@RunWith注解与默认JUnit5版本不兼容的问题,包括删除不必要的依赖和配置Spring Boot测试套件。

新建了一个2.2.6.RELEASE版本的springboot项目,在用单元测试的时候,发现没有@RunWith注解,于是,就添加了

 

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>

好,现在有这个注解了,运行测试方法,报错java.lang.NoClassDefFoundError: org/junit/platform/engine/support/filter/ExclusionReasonConsumingFilter
 

警告: TestEngine with ID 'junit-vintage' failed to discover tests

java.lang.NoClassDefFoundError: org/junit/platform/engine/support/filter/ExclusionReasonConsumingFilter

at org.junit.vintage.engine.discovery.VintageDiscoverer.createTestClassPredicate(VintageDiscoverer.java:83)

at org.junit.vintage.engine.discovery.VintageDiscoverer.collectTestClasses(VintageDiscoverer.java:61)

at org.junit.vintage.engine.discovery.VintageDiscoverer.discover(VintageDiscoverer.java:51)

at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:65)

at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:130)

at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:117)

at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:82)

at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:48)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.support.filter.ExclusionReasonConsumingFilter

at java.net.URLClassLoader.findClass(URLClassLoader.java:382)

at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)

at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

... 11 more

其实实际原因是,这个版本的springboot此时默认的是JUnit5,而@RunWith是junit4里面的东西。解决方式有两种,要么你不要用这个@RunWith注解,要么你把pom文件里面的那个org.junit.vintage注释掉。

 

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

<!--<exclusions>-->

<!--<exclusion>-->

<!--<groupId>org.junit.vintage</groupId>-->

<!--<artifactId>junit-vintage-engine</artifactId>-->

<!--</exclusion>-->

<!--</exclusions>-->

</dependency>

完整代码可看

https://mp.youkuaiyun.com/console/editor/html/108922901

`java.lang.NoClassDefFoundError: org/springframework/core/NestedIOException` 是一种运行时错误,表明在运行时找不到某个类的定义,而该类在编译时是存在的。在 Spring 框架中,`NestedIOException` 是早期版本中用于封装 IO 异常的一个类,但在 Spring 5.3.x 版本中已经被废弃,并在 Spring 6.0 及更高版本中完全移除[^2]。 ### 原因分析 1. **Spring 框架版本与依赖不兼容** 如果项目中使用的是 Spring 6.0 或更高版本,而某些依赖(如 MyBatis 或 MyBatis-Plus)仍然依赖于 `NestedIOException`,就会导致此类错误。这是因为 `NestedIOException` 已从 Spring 框架中移除[^2]。 2. **MyBatis 或 MyBatis-Plus 版本过旧** 旧版本的 MyBatis(尤其是 MyBatis-Spring 集成模块)可能仍然引用了 `NestedIOException`。如果未更新这些依赖以适配新的 Spring 版本,将导致运行时类找不到的问题[^1]。 3. **依赖管理冲突或类路径问题** Maven 或 Gradle 项目中可能存在多个版本的 Spring 或 MyBatis 依赖,导致类路径冲突。例如,不同模块可能引入了不同版本的 Spring 核心库,造成运行时加载错误的类版本[^1]。 --- ### 解决方案 1. **升级 MyBatis 或 MyBatis-Plus 到兼容版本** 确保使用的 MyBatis-Spring 或 MyBatis-Plus 版本与当前 Spring Boot 版本兼容。例如,使用 Spring Boot 3.x 时,应选择支持 Jakarta EE 9 及以上规范的 MyBatis 版本: ```xml <!-- Maven 示例 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> <!-- 确保适配 Spring Boot 3.x --> </dependency> ``` 2. **检查并统一 Spring Boot 与 Spring Cloud Alibaba 的版本兼容性** 如果项目中使用了 Spring Cloud Alibaba,确保其版本与 Spring Boot 的版本匹配。例如,Spring Boot 2.4.2 应搭配 Spring Cloud Alibaba 2021.1[^3]。 3. **排除冲突依赖** 如果存在多个 Spring 版本,可以通过 Maven 的 `<exclusion>` 或 Gradle 的 `exclude` 语句排除旧版本依赖: ```xml <!-- Maven 排除示例 --> <dependency> <groupId>some-group</groupId> <artifactId>some-artifact</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> </exclusions> </dependency> ``` 4. **更新项目整体依赖版本** 使用 Spring Boot 的依赖管理工具(如 `spring-boot-starter-parent`)来统一管理所有 Spring 相关依赖版本,避免手动指定不同版本造成冲突。 --- ### 版本兼容性建议 | Spring Boot 版本 | MyBatis-Spring-Boot-Starter 版本 | Spring Cloud Alibaba 版本 | |------------------|----------------------------------|----------------------------| | 2.4.x | 2.2.0+ | 2.2.6.RELEASE | | 2.5.x | 2.2.1+ | 2.2.7.RELEASE | | 3.0.x | 3.0.x | 2022.1+ | --- ### 代码示例:Maven 依赖配置 ```xml <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- MyBatis Spring Boot Starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> <!-- 数据库驱动示例 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> </dependencies> ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值