java.lang.NoClassDefFoundError: io/restassured/map

2020-07-01 03:28发布

问题:

I am getting error when I try to execute script with rest assured framework. Please guide me to resolve the same. And I used below jars

Java version - 8
rest-assured-2.8.0
json-path-2.8.0
hamcrest-all-1.3
commons-lang3-3.0
json-schema-validator-2.2.0



>FAILED: foo
    java.lang.NoClassDefFoundError: io/restassured/mapper/factory/GsonObjectMapperFactory
        at io.restassured.config.RestAssuredConfig.<init>(RestAssuredConfig.java:41)
        at io.restassured.RestAssured.<clinit>(RestAssured.java:420)
        at practice.GetRequest.foo(GetRequest.java:12)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
        at org.testng.TestRunner.privateRun(TestRunner.java:744)
        at org.testng.TestRunner.run(TestRunner.java:602)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
        at org.testng.SuiteRunner.run(SuiteRunner.java:289)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
        at org.testng.TestNG.runSuites(TestNG.java:1144)
        at org.testng.TestNG.run(TestNG.java:1115)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
    Caused by: java.lang.ClassNotFoundException: io.restassured.mapper.factory.GsonObjectMapperFactory
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 28 more


    ===============================================
        Default test
        Tests run: 1, Failures: 1, Skips: 0
    ===============================================


    ===============================================
    Default suite
    Total tests run: 1, Failures: 1, Skips: 0
    ===============================================

回答1:

The root cause of this is rest-assured *ObjectMapperFactory package names changing, for example between versions 3.x and 4.x.

For anyone coming across this after the release of rest-assured 4.0.0, this problem can appear in Spring Boot projects - caused by a version mismatch between rest-assured and its transitive dependencies on json-path and xml-path in the spring-boot-dependencies bom.

If you specify a dependency io.rest-assured:rest-assured:4.0.0, you also need to explicitly include io.rest-assured:json-path:4.0.0 and io.rest-assured:xml-path:4.0.0, otherwise spring-boot-dependencies will pull in version 3.1.1 with the old *ObjectMapperFactory package names.



回答2:

As Jim Riordan said you have to explicitly include io.rest-assured:json-path:4.0.0 and io.rest-assured:xml-path:4.0.0

I also needed to add an exclusion in my maven dependency io.rest-assured:spring-mock-mvc

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>spring-mock-mvc</artifactId>
    <version>4.0.0</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>rest-assured</artifactId>
            <groupId>io.rest-assured</groupId>
        </exclusion>
    </exclusions>
</dependency>


回答3:

For anyone running into this I found this git page helpful as well : https://github.com/rest-assured/rest-assured/issues/1168

And an example of my maven POM that ended up working is this :

            <dependency>
                <groupId>io.rest-assured</groupId>
                <artifactId>rest-assured</artifactId>
                <version>${io-rest-assured.version}</version>
                <scope>test</scope>
            </dependency>
            <!--Added required depeendencies due to : https://github.com/rest-assured/rest-assured/issues/1168 -->
            <dependency>
                <groupId>io.rest-assured</groupId>
                <artifactId>rest-assured-common</artifactId>
                <version>${io-rest-assured.version}</version>
            </dependency>
            <dependency>
                <groupId>io.rest-assured</groupId>
                <artifactId>json-path</artifactId>
                <version>${io-rest-assured.version}</version>
            </dependency>
            <dependency>
                <groupId>io.rest-assured</groupId>
                <artifactId>xml-path</artifactId>
                <version>${io-rest-assured.version}</version>
            </dependency>


回答4:

In looking at the error, You need to put GSON in your classpath or POM dependancy section explicitly since it's an optional dependency to Rest Assured.

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.1</version>
</dependency>