简体   繁体   中英

Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;

I'm trying to run the test with wiremock on Intellij but getting the error . Could someone please help me with that issue?

    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);
    }
}

here is the error that i get: Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader; at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:31) at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:42) at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:43)

here is my 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>

It seems you have a dependency clash between wiremock and junit5. When I look at the wiremock dependencies on mvncentral, it states it uses xmlunit underneath ( https://mvnrepository.com/artifact/com.github.tomakehurst/wiremock/2.26.3 ).

Following that dependency, you can see xmlunit uses Junit4 underneath ( https://mvnrepository.com/artifact/org.xmlunit/xmlunit-core/2.6.2 ).

I can quite find in which version the 'getDefaultClassloader' method is available from reading the public JUnit APIs. I suggest you try and remove the dependency on junit5 and use junit4 for your testing, to see if that fixes your classloading issue.

If you are using a dependency manager (mvn, gradle), you can list your effective dependencies using command line or your IDE. If you see clashing versions of junit, investigate on that path.

edit: also, welcome to the infamous "dependency hell"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM