Java 使用 RestAssured post 调用的安全异常

Java Security Exception using RestAssured post calls

我在使用 RestAssured 调用 post 时遇到异常。

java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(ClassLoader.java:891)
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:661)
at java.lang.ClassLoader.defineClass(ClassLoader.java:754)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access0(URLClassLoader.java:74)
at java.net.URLClassLoader.run(URLClassLoader.java:369)
at java.net.URLClassLoader.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:134)
at io.restassured.internal.ValidatableResponseOptionsImpl.statusCode(ValidatableResponseOptionsImpl.java:89)
at io.restassured.internal.ValidatableResponseImpl.super$statusCode(ValidatableResponseImpl.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:101)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1217)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:144)
at io.restassured.internal.ValidatableResponseImpl.statusCode(ValidatableResponseImpl.groovy:142)
at io.restassured.internal.ValidatableResponseImpl.statusCode(ValidatableResponseImpl.groovy)

我在 pom.xml 文件中使用以下依赖项

黄瓜-java 4.8.0 黄瓜-junit 4.8.0 io.rest-放心4.1.2 杰克逊数据绑定 2.10.1 commons-io 2.6

我正在使用下面的代码调用 API

    FileInputStream fileInputStream = new FileInputStream( new File("jsonFileInput\pricingAPI.json"));
    RestAssured.baseURI ="my base url here";
    
    given().header("Content-Type","application/json")
    .and()
    .body(IOUtils.toString(fileInputStream,"UTF-8"))
    .when()
    .post("v/pr")
    .then()
    .statusCode(200)
    .and()
    .log().all();

请关注

hamcrest tests always fail

https://code.google.com/archive/p/hamcrest/issues/128

合并解决方案 - 更改 java 构建路径中 jar 的顺序 - 您会发现 JUnit jar 在 hamcrest.jar 之上。您可以在此处将 hamcrest 移动到 JUnit jar 上方,问题就会消失。