java.lang.NoClassDefFoundError : io/restassured/path/json/mapper/factory/JsonbObjectMapperFactory 错误

标签 java jar testng rest-assured web-api-testing

在使用 restassured jar 发出 GET api 请求时,我的程序在以 TestNg 运行时抛出以下错误

输出:

java.lang.NoClassDefFoundError: io/restassured/path/json/mapper/factory/JsonbObjectMapperFactory
    at io.restassured.config.RestAssuredConfig.<init>(RestAssuredConfig.java:41)
    at io.restassured.RestAssured.<clinit>(RestAssured.java:421)
    at SimpleGetTest.GetWeatherDetails(SimpleGetTest.java:13)
    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:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.ClassNotFoundException: io.restassured.path.json.mapper.factory.JsonbObjectMapperFactory
    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

我的java程序如下所示

SimpleGetTest.java

import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class SimpleGetTest {

    @Test
    public void GetWeatherDetails()
    {   
        // Specify the base URL to the RESTful web service
        RestAssured.baseURI = "http://restapi.demoqa.com/utilities/weather/city/";

        // Get the RequestSpecification of the request that you want to sent
        // to the server. The server is specified by the BaseURI that we have
        // specified in the above step.
        RequestSpecification httpRequest = RestAssured.given();

        // Make a request to the server by specifying the method Type and the method URL.
        // This will return the Response from the server. Store the response in a variable.
        Response response = httpRequest.request(Method.GET, "/Hyderabad");

        // Now let us print the body of the message to see what response
        // we have received from the server
        String responseBody = response.getBody().asString();
        System.out.println("Response Body is =>  " + responseBody);

    }

}

使用的外部 jar:

  1. 放心-4.2.0
  2. json-path-4.0.0
  3. xml-path-4.0.0
  4. groovy-2.4.7
  5. commons-lang3-3.0
  6. httpmime-4.3.1

最佳答案

如果您使用 rest-assured版本 4.2.0 那么你应该保留所有相关的 JAR,如 json-pathxml-path相同版本和

作为最佳实践,我们使用了最新的 jars,其中包含大部分错误修复 看起来你需要使用最新版本的 json-path 现在你可以使用

io.rest-assured:json-path:4.3.0
io.rest-assured:xml-path:4.3.0

还要确保您的运行时环境类路径包含这些 JAR,并且在编译时使用相同的版本。

编辑:

还包括

io.rest-assured:rest-assured-common:4.3.0

下面是您需要的 JAR 列表

enter image description here

对于 PKIX 错误

如果您的 JVM 参数中存在 -Djavax.net.ssl.trustStore,Java 将使用该参数指定的 keystore 。您可以通过运行 SSLPoke 测试并指定相同的 JVM 参数来使用该 keystore 来验证 -Djavax.net.ssl.trustStore 参数是否导致问题。

例如:

$JAVA_HOME/bin/java -Djavax.net.ssl.trustStore=/my/custom/truststore SSLPoke jira.example.com 443

您正在尝试访问一个安全的 URL。因此您需要先在您的 keystore 中导入公共(public)证书。

关于java.lang.NoClassDefFoundError : io/restassured/path/json/mapper/factory/JsonbObjectMapperFactory 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62371283/

相关文章:

java - 在方法内部使用静态调用设计 Java 单元测试

java - TestNG 中的默认监听器是什么,我在哪里可以找到这个列表?

java - 从外部运行的 jar 输出到 Eclipse 控制台

java - 通过 Scala 中的套接字发送 Jar 文件时会损坏

java - Jenkins 和 Maven : how to package some files in my JAR and unpack them before running a test?

java - 从图像中识别井字棋盘的状态

java - ant 如何编译和 jar 字节相同的 jar 文件,即 MD5 匹配,除非 .java(以及 .class)发生变化?

java - 临时禁用/删除 catch block 的 Eclipse 插件

java - 删除示例包仍然出现编译错误

java - 如何从 Spring Controller 返回字节数组作为图像,以便浏览器可以显示它?