问题描述
初始化项目时出现 JUnit 报错,具体异常如下:
Exception in "main" java.lang.NoSuchMethodError: 'java.lang.String org.junit.platform.engine.discovery.MethodSelector.getMethodParameterTypes()' at com.intellij.junit5.JUnit5TestRunnerUtil.loadMethodByReflection(JUnit5TestRunnerUtil.java:127)...
原因分析
该 java.lang.NoSuchMethodError 错误表明在运行时,JVM 尝试调用 org.junit.platform.engine.discovery.MethodSelector.getMethodParameterTypes() 方法,但当前加载的 MethodSelector 类版本中并没有这个方法。这通常是由于 JUnit 平台库的不同版本之间存在不兼容所致。
解决方案
可在 pom.xml 文件中修改 parent 的版本号,调整为兼容的版本(例如 Spring Boot 2.7.17):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
<relativePath/>
</parent>

