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 小时前
封装MinIO为starter
java·数据库·spring boot
Zww08913 小时前
idea配置注释模板
java·ide·intellij-idea
却话巴山夜雨时i4 小时前
Java大厂面试:从Spring Boot到微服务的深度剖析
java·spring boot·spring cloud·微服务·分布式事务·大厂面试
希望永不加班4 小时前
SpringBoot 缓存注解:@Cacheable/@CacheEvict 使用
java·spring boot·spring·缓存·mybatis
源码站~4 小时前
基于Spring Boot+Vue3的烹饪交流学习系统 设计与实现
java·vue.js·spring boot·后端·mysql·毕业设计·毕设
zihao_tom4 小时前
Spring Boot 整合 Druid 并开启监控
java·spring boot·后端
鬼先生_sir5 小时前
SpringBoot-源码剖析
java·spring boot·springboot源码解析
wb0430720116 小时前
使用 Java 开发 MCP 服务并发布到 Maven 中央仓库完整指南
java·开发语言·spring boot·ai·maven
nbwenren17 小时前
Springboot中SLF4J详解
java·spring boot·后端