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>