You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Response取值验证时遇JsonPath参数未定义错误求助

问题排查:JsonPath提取带空格的字段时报错

尝试从接口返回的Response对象中提取数据做结果验证时,抛出了IllegalArgumentException,提示参数"H96"未定义。

原代码

@And("^Delete record for member 0000000555for SC_1_1 scenario")
public void deleteClaimsInserted_SC_1_1() {

  Response deleteClaimsInsertedSC1 = SerenityRest.given().auth().oauth2(RestAssuredOAuth2.access_token_code)
           .header("platform", "DR2")
           .contentType("application/json")
           .header("Accept", "application/json")
           .queryParam("mbrId", "000000055564627500")
           .queryParam("custId", "444")
           .queryParam("clntId", "2")
           .queryParam("grpId", "1")
           .queryParam("bnftPrdBgnDt", "2019-01-01")
           .get(deleteClaimsURI)
           .then()
           .statusCode(200)
           .extract().response();


   int validateClaimsDeleteSuccess = deleteClaimsInsertedSC1.jsonPath().getInt("deleted H96 records");
   System.out.println(validateClaimsDeleteSuccess);
   if (validateClaimsDeleteSuccess > 0) {
       System.out.println(successfullDeletion);
   } else {
       System.out.println(failedDeletion);
   }


   softAssert.assertAll();
}

报错信息

失败场景:
C:/Users/dt234939/IdeaProjects/adjudication-automation/src/test/resources/features/adjcpe/comAccum/SCR.feature:6 # SIT_MVP1_PI5_ADJCPE_SC_1_1 - SCR  Phase - UNDER limit

1个场景(1个失败)
5个步骤(1个失败,4个通过)
0m29.568s

**java.lang.IllegalArgumentException: The parameter "H96" was used but not defined. Define parameters using the JsonPath.params(...) function**
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:237)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:257)
    at io.restassured.internal.path.json.JSONAssertion.getAsJsonObject(JSONAssertion.groovy:50)
    at io.restassured.internal.path.json.JSONAssertion$getAsJsonObject.callCurrent(Unknown Source)
    at io.restassured.internal.path.json.JSONAssertion.getResult(JSONAssertion.groovy:28)
    at io.restassured.path.json.JsonPath.get(JsonPath.java:203)
    at io.restassured.path.json.JsonPath.getInt(JsonPath.java:237)
    at com.domanirx.automation.adjudication.adjcpe.comAccum.steps.steps.deleteClaimsInserted_SC_1_1(steps.java:6801)
    at ✽.Delete claim record for member 000000055564627500 for SC_1_1 scenario(C:/Users/dt234939/IdeaProjects/adjudication-automation/src/test/resources/features/adjcpe/comAccum/SCR.feature:10)
Caused by: groovy.lang.MissingPropertyException: No such property: H96 for class: Script1
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:67)
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:51)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:309)
    at Script1.run(Script1.groovy:1)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:441)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:479)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:450)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrap.invoke(PogoMetaMethodSite.java:179)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:70)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
    at io.restassured.internal.path.json.JSONAssertion.eval(JSONAssertion.groovy:80)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:190)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:58)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:184)
    at io.restassured.internal.path.json.JSONAssertion.getAsJsonObject(JSONAssertion.groovy:46)
    at io.restassured.internal.path.json.JSONAssertion$getAsJsonObject.callCurrent(Unknown Source)
    at io.restassured.internal.path.json.JSONAssertion.getResult(JSONAssertion.groovy:28)
    at io.restassured.path.json.JsonPath.get(JsonPath.java:203)
    at io.restassured.path.json.JsonPath.getInt(JsonPath.java:237)
    at com.domanirx.automation.adjudication.adjcpe.comAccum.steps.steps.deleteClaimsInserted_SC_1_1(steps.java:6801)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at cucumber.runtime.Utils$1.call(Utils.java:31)
    at cucumber.runtime.Timeout.timeout(Timeout.java:16)
    at cucumber.runtime.Utils.invoke(Utils.java:25)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:37)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:40)
    at cucumber.api.TestStep.executeStep(TestStep.java:102)
    at cucumber.api.TestStep.run(TestStep.java:83)
    at cucumber.api.TestCase.run(TestCase.java:58)
    at cucumber.runner.Runner.runPickle(Runner.java:80)
    at cucumber.runtime.Runtime.runFeature(Runtime.java:119)
    at cucumber.runtime.Runtime.run(Runtime.java:104)
    at cucumber.api.cli.Main.run(Main.java:36)
    at cucumber.api.cli.Main.main(Main.java:18)

Process finished with exit code 1

问题原因

RestAssured的JsonPath基于Groovy实现,当字段名包含空格或类似变量的字符(比如这里的"H96")时,直接传入字段名会被Groovy解析为表达式,错误地将"H96"识别为未定义的变量,导致抛出异常。

解决方法

将包含空格的字段名用单引号包裹,或者使用方括号语法,让JsonPath正确识别为完整的字段名:

方式1:单引号包裹字段名

int validateClaimsDeleteSuccess = deleteClaimsInsertedSC1.jsonPath().getInt("'deleted H96 records'");

方式2:方括号语法

int validateClaimsDeleteSuccess = deleteClaimsInsertedSC1.jsonPath().getInt("[\"deleted H96 records\"]");

修改后,JsonPath会将整个字符串作为字段名解析,不会再把"H96"当成独立变量,即可正常提取数据。

内容的提问来源于stack exchange,提问作者Muazzam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 23:25:27