Java NoSuchMethodError:org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass

Java NoSuchMethodError:org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass,java,intellij-idea,dependencies,junit5,junit-jupiter,Java,Intellij Idea,Dependencies,Junit5,Junit Jupiter,我的测试会导致错误。我尝试在IntelliJ Idea2018.3.2中执行它。所有jupiter和junit依赖项都有版本RELEASE 错误全文如下: Dec 26, 2018 1:17:17 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests java.lang.NoSuc

我的测试会导致错误。我尝试在IntelliJ Idea
2018.3.2
中执行它。所有jupiter和junit依赖项都有版本
RELEASE

错误全文如下:

Dec 26, 2018 1:17:17 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(Ljava/lang/String;)Lorg/junit/platform/commons/function/Try;
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.createAbortedExecutionPredicate(OpenTest4JAndJUnit4AwareThrowableCollector.java:40)
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.<clinit>(OpenTest4JAndJUnit4AwareThrowableCollector.java:30)
at org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector(JupiterThrowableCollectorFactory.java:34)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:68)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
我的pom.xml的一部分

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
</parent>

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit-bom</artifactId>
        <version>RELEASE</version>
        <type>pom</type>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>

org.springframework.boot
spring启动程序父级
2.1.1.1发布
org.springframework.boot
弹簧起动试验
测试
org.junit.jupiter
JUnitJupiter api
释放
测试
org.junit
junit bom
释放
聚甲醛
测试
org.mockito
莫基托朱庇特酒店
释放
测试

如何解决此问题?

我将jupiter和junit的版本更改为5.3.2,问题已经解决了

在添加平台和启动器依赖项后,我能够运行junit 5测试:

<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-commons -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-commons</artifactId>
    <version>1.4.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.4.0</version>
    <scope>test</scope>
</dependency>

org.junit.platform
junit平台共享
1.4.0
org.junit.platform
junit平台发射器
1.4.0
测试

spring boot 2.1.3和junit 5.4.0测试所需的最低pom.xml设置下一步:


org.springframework.boot
spring启动程序父级
2.1.3.1发布
5.4.0
org.junit.jupiter
JUnitJupiter api
测试
org.junit.jupiter
朱尼特木星发动机
测试
org.springframework.boot
弹簧起动试验
测试
使用:


org.junit.jupiter

Maven/Gradle、Eclipse/IntelliJ的组合使用Spring Boot/或not,同时包括Junit4/Junit5,会抛出不同风格的NoSuchMethodError。这导致了许多不同的解决方案,所有这些都只解决了人们的一部分问题。这个冗长的答案试图解释根本原因,以及您可以采取哪些步骤来解决问题,无论您使用哪种口味

根本原因:
在编译时,使用了一个版本的ReflectionUtils(我在这里也看到了其他类),在运行时使用了另一个版本。运行时版本希望某个方法存在,但该方法可能不存在,或者存在时具有不同的签名(参数的数量/类型)

请注意,当我说编译时时,这可能意味着(通常是)用于在类路径内编译第三方jar的ReflectionUtils版本

解决方案总是更改或排除某个jar的某个版本。诀窍是找到哪个jar和哪个版本

要解决的问题:
首先,确定在运行时使用的ReflectionUtils(或它所抱怨的其他类)的版本。这相当容易。在eclipse中,执行打开类型,在IntelliJ中,使用导航/转到类。确保完全限定路径。如果路径上正好有一个版本,这是很好的。如果路径上有多个版本,则要采取的第一个操作是修改生成路径以排除其中一个版本

如果无法修改生成路径,即使是临时修改,也很难确定运行时使用的ReflectionUtils版本。由于JUnit4和JUnit5在您可以运行任何代码之前就开始和结束了,所以您不能插入

CodeSource src = ReflectionUtils.class.getProtectionDomain().getCodeSource();
if (src != null) {
    URL jar = src.getLocation();
    System.out.println(jar.toString());
}
获取运行时jar的位置。但你可以试试:

1-创建一个新的项目,一个新的pom,在其中放一个junit,用上面的代码运行它,看看结果如何。这可能有效,因为非工作项目中的任何冲突都已消除。在这种情况下,移动到#2可能不起作用。这也可能给出错误的结果,因为新项目的运行时路径可能不同于非工作项目的运行时路径。尝试更新新项目的pom,使其看起来越来越像非工作项目的pom。记住,目标是找到在运行时使用的类的版本

2-使用谷歌。是的,我知道你是通过谷歌来这里的,但是这次你要改变你的搜索,看看是否有人记录了“这个版本的JUnit5在运行时使用这个版本的ReflectionUtils”或者“这个版本的IntelliJ将这个版本的ReflectionUtils放在类路径上”或者“此版本的Spring Boot正在将此版本的ReflectionUtils放在类路径上”。请记住,目标是查找运行时使用的类的版本

3-劫持一个类(高级)。当stacktrace出现时,您将在stacktrace中看到几个类。尝试在internet上查找该类的源代码,并在您的项目中创建该包和类。然后插入上面的代码并尝试运行失败的测试

一旦找到运行时使用的ReflectionUtils版本,就需要找出编译时使用的版本

确定哪一个罐子的反射率在里面

在运行测试时弹出的stacktrace中,确定试图使用ReflectionUtils时死掉的顶级类和方法。这是使用不兼容版本的ReflectionUtils编译的代码。找出该类中的jar。我将此jar称为冲突jar。尝试获取该冲突的依赖关系树icting jar。依赖关系树应该指示冲突jar依赖的ReflectionUtils jar版本

一旦您发现在编译时和运行时使用了哪个版本的ReflectionUtils jar,您可能会发现它们是不同的,而且不兼容

1-更改运行时使用的ReflectionUtils jar的版本,以匹配冲突jar所需的ReflectionUtils版本

2-将冲突jar的版本更改为依赖于运行时路径上相同版本的ReflectionUtils jar的版本

3-如果冲突jar是某个
CodeSource src = ReflectionUtils.class.getProtectionDomain().getCodeSource();
if (src != null) {
    URL jar = src.getLocation();
    System.out.println(jar.toString());
}