45

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
    ===============================================
Sal-laS
  • 11,016
  • 25
  • 99
  • 169
user3415045
  • 491
  • 1
  • 6
  • 12

6 Answers6

74

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.

Jim Riordan
  • 1,378
  • 11
  • 18
  • 1
    This helped me out! Rather than manually specifying the dependency, you can just override the BOM property `extra["rest-assured.version"] = "4.0.0"` (Kotlin DSL) – Cisco Jul 02 '19 at 18:31
20

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>
Abe
  • 229
  • 3
  • 7
  • 2
    This helped me get past the Gson errors and then ran into Groovy classpath errors and also needed to specify org.codehaus.groovy:Groovy:3.0.2 as Spring Boot test dependencies was bringing in an incompatible version 2.5. This is what I experienced with rest-assured 4.3.0. – GameSalutes Jun 20 '20 at 21:18
  • Along with Jim Riordan's answer, this helped me because it made me aware of rest-assured-common, which also needs to be specified. – Corwin Newall May 24 '23 at 13:54
4

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>
Hamarkhis
  • 111
  • 1
  • 4
4

Another options is to define dependency on rest-assured-all

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

And exclude 3.X version if you are using Spring Mock MVC

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>spring-mock-mvc</artifactId>
    <version>3.3.0</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <artifactId>rest-assured</artifactId>
            <groupId>io.rest-assured</groupId>
        </exclusion>
    </exclusions>
</dependency>
fg78nc
  • 4,774
  • 3
  • 19
  • 32
0

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>
TJ O'Hare
  • 62
  • 2
0

For Maven projects, the code below works well with Spring Boot projects:

<properties>
       <groovy.version>3.0.7</groovy.version>
       <rest-assured.version>4.3.3</rest-assured.version>
   </properties>

   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>io.rest-assured</groupId>
           <artifactId>spring-mock-mvc</artifactId>
           <scope>test</scope>
           <exclusions>
               <exclusion>
                   <artifactId>groovy</artifactId>
                   <groupId>org.codehaus.groovy</groupId>
               </exclusion>
           </exclusions>
       </dependency>
       <dependency>
           <groupId>org.codehaus.groovy</groupId>
           <artifactId>groovy</artifactId>
           <version>${groovy.version}</version>
       </dependency>
       <dependency>
           <groupId>org.codehaus.groovy</groupId>
           <artifactId>groovy-xml</artifactId>
           <version>${groovy.version}</version>
       </dependency>
       <dependency>
           <groupId>org.codehaus.groovy</groupId>
           <artifactId>groovy-json</artifactId>
           <version>${groovy.version}</version>
       </dependency>
       <dependency>
           <groupId>io.rest-assured</groupId>
           <artifactId>json-path</artifactId>
           <version>${rest-assured.version}</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>io.rest-assured</groupId>
           <artifactId>xml-path</artifactId>
           <version>${rest-assured.version}</version>
           <scope>test</scope>
       </dependency>
   </dependencies>
Moncef AOUDIA
  • 647
  • 8
  • 14