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>
相关推荐
拽着尾巴的鱼儿13 小时前
Idea:Cannot Run Git 无法运行git
java·git·intellij-idea
编程_大白13 小时前
IDEA配置SQL方言
java·sql·intellij-idea
摇滚侠14 小时前
《Spring Boot 3:高级与架构设计》第 2 章 IOC容器的高级机制 Environment 阅读笔记 4
spring boot·笔记·后端
豆角焖肉14 小时前
Spring Boot配置文件与自动配置原理深度剖析
spring boot·配置文件·自动配置原理·yaml·conditional
豆角焖肉14 小时前
Spring Boot入门实战:从零搭建第一个应用
java·spring boot·后端·自动配置
BUG指挥官14 小时前
Redis已接入AI
数据库·人工智能·spring boot·redis·spring cloud
m0_587383001 天前
全民健身解决方案软件开发实战:从架构设计到落地指南
java·spring boot·spring·架构·需求分析
蓝桉柒71 天前
下载idea,用idea输入一个程序
java·ide·intellij-idea
azhou的代码园1 天前
基于RFM模型的中小型企业客户管理系统
java·人工智能·spring boot·毕业设计
行百里er1 天前
BeanPostProcessor:包装 Feign Client
spring boot·后端·监控