java - 使用 Rest Assured RequestSpecification 和 testng 时出错

标签 java testng rest-assured

我正在使用放心版本 2.3.4 和 testng 来测试 REST API,但出现以下错误

java.lang.IncompatibleClassChangeError: the number of constructors during runtime and compile time for com.jayway.restassured.internal.RequestSpecificationImpl$EncodingTarget do not match. Expected -1 but got 2
    at groovy.lang.MetaClassImpl.selectConstructorAndTransformArguments(MetaClassImpl.java:1400)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.selectConstructorAndTransformArguments(ScriptBytecodeAdapter.java:234)
    at com.jayway.restassured.internal.RequestSpecificationImpl$EncodingTarget.$INIT(RequestSpecificationImpl.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:43)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:99)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:50)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:157)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:169)
    at com.jayway.restassured.internal.RequestSpecificationImpl$EncodingTarget.<clinit>(RequestSpecificationImpl.groovy)
    at sun.misc.Unsafe.ensureClassInitialized(Native Method)
    at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
    at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:140)
    at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1057)
    at java.lang.reflect.Field.getFieldAccessor(Field.java:1038)
    at java.lang.reflect.Field.get(Field.java:379)
    at org.codehaus.groovy.reflection.CachedField.getProperty(CachedField.java:51)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1552)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3308)
    at org.codehaus.groovy.runtime.callsite.ClassMetaClassGetPropertySite.getProperty(ClassMetaClassGetPropertySite.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227)
    at com.jayway.restassured.internal.RequestSpecificationImpl$_applyPathParamsAndEncodePath_closure24.doCall(RequestSpecificationImpl.groovy:1438)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:885)
    at groovy.lang.Closure.call(Closure.java:405)
    at org.codehaus.groovy.runtime.DefaultGroovyMethods.inject(DefaultGroovyMethods.java:2983)
    at org.codehaus.groovy.runtime.dgm$328.invoke(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:271)
    at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
    at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndEncodePath(RequestSpecificationImpl.groovy:1405)
    at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$applyPathParamsAndEncodePath(RequestSpecificationImpl.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1055)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:885)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:705)
    at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy)
    at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:45)
    at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.java:55)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:46)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:145)
    at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1372)
    at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1055)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:885)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:705)
    at com.jayway.restassured.internal.RequestSpecificationImpl.invokeMethod(RequestSpecificationImpl.groovy)
    at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.call(PogoInterceptableSite.java:45)
    at org.codehaus.groovy.runtime.callsite.PogoInterceptableSite.callCurrent(PogoInterceptableSite.java:55)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:46)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:149)
    at com.jayway.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy:155)
    at com.jayway.restassured.internal.RequestSpecificationImpl.post(RequestSpecificationImpl.groovy)
    at com.alu.mdf.test.common.RestCommonOperations.restPostOperation(RestCommonOperations.java:77)
    at com.alu.mdf.test.common.Customers.customersBaseOperations(Customers.java:52)
    at com.alu.mdf.testsuite.sure.rest.TestCustomerResource1.preRequisites(TestCustomerResource1.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

我的方法如下所示,

public Response restPostOperation(String resource,String PayLoad) throws IOException{

        String  methodName="restPostOperation";
        log.info("Executing the method {} in class {} begins",methodName,className);
        try{
            RequestSpecBuilder builder = new RequestSpecBuilder();
            //Setting content type as application/json or application/xml
            builder.setContentType("application/xml; charset=UTF-8");
            //Setting Request body  
            builder.setBody(PayLoad);
            RequestSpecification requestSpec = builder.build();
            configurePreResourcePath();
            response=given().spec(requestSpec).when().post(resource);
            log.info("Executing the method {} in class {} ends",methodName,className);
            return  response;
        }catch(Exception ex){
            System.out.println("Request can not be processed.An Exception has been Occured "+ex);
            throw ex;
        }
    }

我无法弄清楚为什么会出现这个问题。如果您遇到类似的问题,谁可以帮助我

最佳答案

看来您的类路径中可能有错误版本的 groovy。如果您使用其中任何一个库,请尝试将放心的依赖项作为 maven/gradle 中的第一个库。

关于java - 使用 Rest Assured RequestSpecification 和 testng 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26763979/

相关文章:

java - ShowPhoto 类型中的 createBitmapFromURI(uri) 方法不适用于参数(字符串)

java - 尽管有 @ResponseStatus 注释,ResponseEntityExceptionHandler 不会向客户端发送错误代码

java - 无法将 WebDriver 对象传递给 TestNG 中的工厂方法

java - 在 Linux 服务器上运行 selenium 套件

java - JsonProperty 会序列化但不会反序列化

java - 有没有办法比较不同格式的时间戳?

java - 在java中将一个列表转换为另一个列表

java - Android APK 无法安装在设备上

testng - 是否有类似于 Junit 中的 ErrorCollector 的 TestNG 方法

rest-assured - 在rest api中传递url参数时出现"Invalid number of path parameters. Expected 0, was 3"错误(放心)