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>
相关推荐
阿丰资源10 小时前
SpringBoot+Vue实战:打造企业级在线文档管理系统
vue.js·spring boot·后端
0xDevNull10 小时前
Spring Boot 自动装配:从原理到实践
java·spring boot·后端
qq_5895681011 小时前
java学习笔记,包括idea快捷键
java·ide·intellij-idea
小怪吴吴12 小时前
idea 开发Android
android·java·intellij-idea
一次旅行12 小时前
IDEA安装CC GUI新手指南
java·ide·intellij-idea
a8a30213 小时前
Laravel9.x新特性全解析
运维·spring boot·nginx
aLTttY15 小时前
Spring Boot + Redis 实现接口防抖与限流实战指南
spring boot·redis·junit
V+zmm1013416 小时前
毕业设计:基于neo4j的知识图谱的智能问答系统(源码)
spring boot·毕业设计·知识图谱·课程设计·neo4j·智能问答·毕设
直奔標竿17 小时前
Java开发者AI转型第二十三课!Spring AI个人知识库实战(二):异步ETL流水线搭建与避坑指南
java·人工智能·spring boot·后端·spring
浮尘笔记17 小时前
在Snowy后台无需编码实现自动化生成CRUD操作流程
java·开发语言·经验分享·spring boot·后端·程序人生·mybatis