idea中springboot中使用junit测试报错的解决方案

java 复制代码
Exception in thread "main" java.lang.NoSuchMethodError: 'java.lang.String org.junit.platform.engine.discovery.MethodSelector.getMethodParameterTypes()'
	at com.intellij.junit5.JUnit5TestRunnerUtil.loadMethodByReflection(JUnit5TestRunnerUtil.java:127)
	at com.intellij.junit5.JUnit5TestRunnerUtil.buildRequest(JUnit5TestRunnerUtil.java:102)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:43)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)

出现这样的错误,原因是JUnit 版本冲突 / IDEA 与 JUnit Platform 不匹配​ 导致的。

👉 运行时加载的 JUnit Platform 版本太旧

👉 IDEA 的 JUnit5 插件却调用了新版本的方法

✅ 本质:依赖版本不一致

解决方法:

解决方案:换junit4

springboot工程如何使用junit4:

1)修改pom.xml

使用springboot启动器创建的工程的pom中默认会带测试启动器spring-boot-starter-test;

spring-boot-starter-test默认依赖Junit5(JUnit Platform + JUnit Jupiter + JUnit Vintage);

需要排除Junit5相关的依赖包:排除junit-jupiter-api,然后再加入Junit4的依赖;

java 复制代码
<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 小时前
生态整合与实战篇:Spring Boot 整合 RocketMQ 完全指南
spring boot·rocketmq·java-rocketmq
减瓦2 小时前
Jackson 使用指南
java·spring boot·json
江南十四行3 小时前
Maven 学习框架:依赖管理 + 仓库配置 + IDEA 集成
学习·maven·intellij-idea
旺仔学长 哈哈3 小时前
基于SpringBoot的在线招聘测评系统的设计与实现----附源码35253+数据库文档
数据库·spring boot·后端·在线招聘
万亿少女的梦1683 小时前
基于Spring Boot的乐助在线助农系统设计与实现
java·spring boot·mysql·敏感词过滤·助农系统
码农学院14 小时前
基于Spring Boot的跨境电商多语言订单管理系统架构设计
java·大数据·spring boot
whyfail20 小时前
前端学 Spring Boot(5):从“你是谁”到“服务真的上线了”
前端·spring boot·后端
2601_9538246120 小时前
【计算机毕业设计】基于Spring Boot的画师接稿平台设计与实现
java·spring boot·后端
稚南城才子,乌衣巷风流1 天前
Spring Boot 中的 @Lazy 注解:延迟加载的深入解析与实践
java·spring boot·后端
hexu_blog1 天前
springboot3集成shardingsphere4.0 分表分库
java·spring boot·mybatis