我正在向我的服务器发出请求,JSON响应如下所示:
{
"6269f15a0bb9b1b7d86ae718e84cddcd" : {
"attr1":"val1",
"attr2":"val2",
.
.
"attrx":"valx",
}
}
密钥是“用户”的MD5哈希(与此讨论无关)。我此时无法更改此密钥。
当我运行一个放心的测试时,我的请求对象看起来像
{
"fields": [ "attr1","attr2",...,"attr3" ]
"users": [
{
"userId": "2f605845757870234d94ae14ca83c660"
}
]
}
当我运行以下操作时出现运行时错误
Response response =
given().
contentType("application/json").
request().body(requestObj).
when().
post(ipAddress).
then().
extract().response();
response.
then().
body("6269f15a0bb9b1b7d86ae718e84cddcd.attr1",equalTo("blah"));
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unexpected token: 6269f @ line 1, column 27.
6269f15a0bb9b1b7d86ae718e84cddcd
^
1 error
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
at com.jayway.restassured.internal.path.json.JSONAssertion.getAsJsonObject(JSONAssertion.groovy:51)
at com.jayway.restassured.internal.path.json.JSONAssertion$getAsJsonObject.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at com.jayway.restassured.internal.path.json.JSONAssertion.getResult(JSONAssertion.groovy:31)
at com.jayway.restassured.path.json.JsonPath.get(JsonPath.java:183)
at com.nxn.AutomationTests.MDXTest.testSample(MDXTest.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
现在,我认为这是因为JSON被反序列化为Java类,当我要求attr1的值时,它会查找一个类失败,因为类名不能以数字开头。有谁知道我是如何解决这个问题的?
答案 0 :(得分:1)
这实际上是一个错误!我已将其添加到问题跟踪器(https://code.google.com/p/rest-assured/issues/detail?id=318)。
解决方法是转义属性名称:
.. .then().body("\"6269f15a0bb9b1b7d86ae718e84cddcd\".attr1"), equalTo("val1")