java.lang.NoClassDefFoundError: io/restassured/mapper/factory/GsonObjectMapperFactory

前端 未结 5 975
不知归路
不知归路 2021-01-01 12:35

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         


        
相关标签:
5条回答
  • 2021-01-01 12:39

    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.

    0 讨论(0)
  • 2021-01-01 12:49

    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>
    
    0 讨论(0)
  • 2021-01-01 12:52

    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>
    
    0 讨论(0)
  • 2021-01-01 12:56

    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>
    
    0 讨论(0)
  • 2021-01-01 13:02

    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>
    
    0 讨论(0)
提交回复
热议问题