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>
相关推荐
Maiko Star16 小时前
* SpringBoot整合LangChain4j
java·spring boot·后端·langchain4j
绝知此事17 小时前
【产品更名】通义灵码升级为 Qoder CN:AI 编码助手新时代,附大模型收费与 Spring Boot 支持全对比
人工智能·spring boot·后端·idea·ai编程
linmoo198617 小时前
Agent应用实践之四 - 基础:AgentScope-SpringBoot集成源码解析
人工智能·spring boot·agent·agentscope·openclaw
海兰18 小时前
【第21篇-续】graph-Stream-Node改造为适配openAI模型示例
java·人工智能·spring boot·spring·spring ai
北风toto18 小时前
为什么 IntelliJ IDEA Community 无法开发 Vue?——附解决方案
java·vue.js·intellij-idea
wh_xia_jun18 小时前
How to Write Test Cases in Java Application using Mockito and Junit?
junit
Albert Edison19 小时前
基于 SpringBoot + RabbitMQ 完成企业级应用通信
spring boot·rabbitmq·java-rabbitmq
Tirzano19 小时前
批量查询在线成员对应节点redis
数据库·redis·junit
happymaker062620 小时前
Spring学习日记——DAY03(yml文件)
java·spring boot·spring
hikktn21 小时前
企业级Spring Boot应用管理:从零打造生产级启动脚本
java·spring boot·后端