当前位置: 首页 > 知识库问答 >
问题:

将Rest Assured RequestSpecification与testng

况承福
2023-03-14

我使用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;
        }
    }

我不明白为什么会这样。如果你遇到类似的问题,有人能帮我吗

共有1个答案

戚飞
2023-03-14

似乎在类路径中有一个错误的groovy版本。如果您使用Maven/Gradle中的任何一个库,请尝试将rest assured依赖项作为第一个库。

 类似资料:
  • 我正在尝试为我的应用程序实现请求TGT的编程逻辑,因此在通过GSSAPI和GSS-SPNEGO机制对LDAP服务器进行身份验证之前,没有必要从命令行调用。 我创建一个内存ccache,请求一个带有用户名和密码的TGT,然后使用gss\u krb5\u import\u cred导入凭据。在启动身份验证之前,为LDAP结构设置GSSAPI上下文。 下面的示例代码在GSSAPI中运行良好,但当我尝试将

  • 目前,我的应用程序使用SpringMVC进行所有控制器映射。我正在尝试实现一个tinyMCE拼写检查,它包括一个Servlet,我不确定如何在不修改该文件本身的情况下正确集成该Servlet。我想避免修改,这样如果我们以后有新版本,我们就可以了。 Servlet看起来像...

  • 问题内容: 关于通过JAVA程序与AutoCAD接口的任何想法。我正在寻找一种可以直接与AutoCAD会话(甚至开始一个)交互的解决方案,它可以与AutoCAD的.NET扩展一起工作。或通过JAVA使用ObjectARX的任何方法,绝对不能在其上编写完整的JNI包装器。 补充:我们正在寻找开源的东西。如果没有,我们可以准备在开放源代码中创建一个,如果有人可以伸出援手说明如何做到这一点。我们看了JN

  • 问题内容: 我有一个主要使用Richfaces的JSF应用程序。我想介绍一个功能更丰富的网格控件,例如基于jQuery的jqgrid。是否可以从JSF支持bean获得JSON数据?如果没有,是否有替代方法? 问题答案: 这里可能有几个选择。 Bean方法可以返回一个字符串,它只是一个JSON字符串 您可以使用JsonLib将Java对象转换为Json。 使用其他Servlet服务网格请求 我将推荐

  • 问题内容: 我的盒子上正在运行以下Docker容器… 我的目标是让kibana链接到我的elasticsearch容器,但是当我点击kibana时,它告诉我我没有任何文档存储。我知道这是不对的,因为我肯定在Elasticsearch中有文件。我猜我的链接命令是错误的。 这是我用来启动kibana容器的docker命令。 有人可以告诉我我做错了什么吗? 谢谢 问题答案: 首先,链接是旧功能,首先创建

  • 问题内容: 我需要将DB2与Java连接。请帮助我在 ECLIPSE* 中将 DB2 与Java 连接 。如果您可以逐步指导我,将对您有很大帮助。请让我知道如何在Eclipse代码段中添加类路径: * 运行代码时,出现以下异常: 问题答案: 您需要更正软件包名称。 要在项目中添加.jar =>项目+属性+ Java构建路径+选择“库”选项卡+添加外部Jars …