【问题标题】:Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;线程“主”java.lang.NoSuchMethodError 中的异常:org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
【发布时间】:2020-03-07 18:10:41
【问题描述】:

我正在尝试在 Intellij 上使用wiremock 运行测试,但出现错误。有人可以帮我解决这个问题吗?

    public class Tests {

    private WireMockServer wireMockServer;

    @Before
    public void setup() {
        wireMockServer = new WireMockServer(8080);
        wireMockServer.start();
        setupStub();
    }

    @After
    public void teardown() {
        wireMockServer.stop();
    }

    public void setupStub() {
        wireMockServer.stubFor(get(urlEqualTo(PEOPLE_PATH))
                .willReturn(aResponse().withHeader("Content-Type", "text/plain")
                        .withStatus(200)
                        .withBodyFile("/Users/denis/Documents/people/autotests/src/test/resources/_files/json/people.json")));
    }

    @Test
    public void testStatusCodePositive() {
        given().
                when().
                get(BASE_URL + PEOPLE_PATH).
                then().
                assertThat().statusCode(200);
    }
}

这是我得到的错误: 线程“主”java.lang.NoSuchMethodError 中的异常:org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader; 在 org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:31) 在 org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:42) 在 com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:43)

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.people.apitest</groupId>
   <artifactId>people-apitest</artifactId>
   <version>1.0-SNAPSHOT</version>

   <dependencies>

      <dependency>
         <groupId>com.github.tomakehurst</groupId>
         <artifactId>wiremock</artifactId>
         <version>2.24.1</version>
      </dependency>

      <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-engine</artifactId>
         <version>5.5.2</version>
         <scope>test</scope>
      </dependency>

      <dependency>
         <groupId>io.rest-assured</groupId>
         <artifactId>rest-assured</artifactId>
         <version>4.1.1</version>
         <scope>test</scope>
      </dependency>

      <dependency>
         <groupId>com.jayway.restassured</groupId>
         <artifactId>json-schema-validator</artifactId>
         <version>2.8.0</version>
      </dependency>
   </dependencies>
</project>

【问题讨论】:

标签: java wiremock


【解决方案1】:

看来您在wiremock 和junit5 之间存在依赖冲突。 当我查看 mvncentral 上的 wiremock 依赖项时,它声明它在下面使用 xmlunit (https://mvnrepository.com/artifact/com.github.tomakehurst/wiremock/2.26.3)。

根据该依赖关系,您可以看到 xmlunit 在下面使用 Junit4 (https://mvnrepository.com/artifact/org.xmlunit/xmlunit-core/2.6.2)。

我可以通过阅读公共 JUnit API 找到“getDefaultClassloader”方法在哪个版本中可用。我建议您尝试删除对 j​​unit5 的依赖并使用 junit4 进行测试,看看是否可以解决您的类加载问题。

如果您使用的是依赖管理器(mvn、gradle),您可以使用命令行或 IDE 列出您的有效依赖。如果您看到 junit 的冲突版本,请在该路径上进行调查。

编辑:另外,欢迎来到臭名昭著的“依赖地狱”

【讨论】: