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

时间:2017-07-09 06:24:42

标签: rest-assured

当我尝试使用放心框架执行脚本时,我收到错误。请指导我解决同样的问题。我用下面的罐子

Java版本 - 8 休息保证的-2.8.0 JSON-路径2.8.0 hamcrest-ALL-1.3 公地lang3-3.0 JSON-架构验证-2.2.0

  

失败:foo       java.lang.NoClassDefFoundError:io / restassured / mapper / factory / GsonObjectMapperFactory           在io.restassured.config.RestAssuredConfig。(RestAssuredConfig.java:41)           在io.restassured.RestAssured。(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)           在org.testng.TestRunner.privateRun(TestRunner.java:744)           在org.testng.TestRunner.run(TestRunner.java:602)           在org.testng.SuiteRunner.runTest(SuiteRunner.java:380)           在org.testng.SuiteRunner.run顺序(SuiteRunner.java:375)           在org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)           在org.testng.SuiteRunner.run(SuiteRunner.java:289)           在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)           在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)           at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)           在org.testng.TestNG.runSuitesLocally(TestNG.java:1226)           在org.testng.TestNG.runSuites(TestNG.java:1144)           在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)           在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)       引起: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个

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


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

6 个答案:

答案 0 :(得分:8)

其根本原因是要保证*ObjectMapperFactory软件包名称的更改,例如在版本3.x4.x之间进行更改。

对于在release of rest-assured 4.0.0之后遇到此问题的任何人,此问题都可能在Spring Boot项目中出现-由rest-assured及其在json-path和{{1 }}中的spring-boot-dependencies bom

如果您指定依赖项xml-path,则还需要显式包括io.rest-assured:rest-assured:4.0.0io.rest-assured:json-path:4.0.0,否则spring-boot依赖项将在旧版本中引入版本io.rest-assured:xml-path:4.0.0 * ObjectMapperFactory包名称。

答案 1 :(得分:1)

正如吉姆·里尔丹(Jim Riordan)所说,您必须明确包括io.rest-assured:json-path:4.0.0io.rest-assured:xml-path:4.0.0

我还需要在我的maven依赖项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>

答案 2 :(得分:1)

另一种选择是定义对rest-assured-all的依赖性

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

如果使用Spring Mock MVC,则排除3.X版本

<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>

答案 3 :(得分:0)

对于遇到此问题的任何人,我也发现此git页面也很有帮助:https://github.com/rest-assured/rest-assured/issues/1168

我的Maven POM最终工作的一个示例是:

            <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 :(得分:0)

对于 Maven 项目,以下代码适用于 Spring Boot 项目:

<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>

答案 5 :(得分:-1)

在查看错误时,您需要明确地将GSON放在类路径或POM依赖部分中,因为它是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>