升级Rest-Assured至5.3.0后遇ContentParser.parse()方法缺失异常
Rest-Assured 5.3.0升级后断言报错问题
问题背景
将应用Tomcat版本升级至10,同步把Rest-Assured从3.3.0升级到5.3.0后,执行接口测试时抛出方法缺失异常,错误指向响应体断言代码。
错误信息
groovy.lang.MissingMethodException: 找不到适用于参数类型(io.restassured.internal.RestAssuredResponseImpl, io.restassured.internal.ResponseParserRegistrar...)的方法io.restassured.internal.ContentParser.parse() 可能的解决方案: wait(), grep(), any() at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:70) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:50) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:166) at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:497) at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source) at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:696) at io.restassured.internal.ResponseSpecificationImpl.this$2$validateResponseIfRequired(ResponseSpecificationImpl.groovy) at jdk.internal.reflect.GeneratedMethodAccessor75.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:48) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:189) at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:57) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185) at io.restassured.internal.ResponseSpecificationImpl.body(ResponseSpecificationImpl.groovy:270) at io.restassured.specification.ResponseSpecification$body$2.callCurrent(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51) at io.restassured.specification.ResponseSpecification$body$2.callCurrent(Unknown Source) at io.restassured.internal.ResponseSpecificationImpl.body(ResponseSpecificationImpl.groovy:117) at io.restassured.internal.ValidatableResponseOptionsImpl.body(ValidatableResponseOptionsImpl.java:244) at com.test.mytest(MyApiTest.java:120) // 后续栈跟踪省略
测试代码
given() .when() .get("/myapi") .then() .statusCode(200) .body("size()", is(3)) // 报错行
当前依赖配置
<dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>5.3.0</version> <scope>test</scope> </dependency>
解决方案
原因分析
这个错误源于Rest-Assured 5.x与项目中Groovy版本冲突,或是缺少必要的JSON解析依赖:
- Rest-Assured 5.x依赖Groovy 3.x,但Tomcat 10或项目其他依赖可能引入了旧版Groovy,导致方法签名不兼容;
- 5.x版本拆分了模块,仅引入核心包可能缺失JSON解析相关组件。
解决步骤
统一Groovy版本并排除冲突依赖
修改依赖配置,排除Rest-Assured自带的Groovy包,显式引入兼容的Groovy JSON模块:<dependency> <groupId>io.restassured</groupId> <artifactId>rest-assured</artifactId> <version>5.3.0</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-json</artifactId> <version>3.0.17</version> <scope>test</scope> </dependency>排查依赖树清理冲突
执行mvn dependency:tree(Maven项目)或./gradlew dependencies(Gradle项目)查看依赖树,移除其他模块引入的旧版Groovy依赖。显式指定响应解析器
在断言前显式指定JSON解析器,避免自动解析时的兼容性问题:given() .when() .get("/myapi") .then() .statusCode(200) .using() .defaultParser(Parser.JSON) .body("size()", is(3));
内容的提问来源于stack exchange,提问作者Jorah Mormont
相关产品推荐
相关产品推荐

