我需要验证像下面这样的响应是否包含一些字段。我对字段值不感兴趣 - 只是对键存在感兴趣。例如,我想检查这种类型的响应中是否存在键“id”。我将如何实现这一目标?
[
{
"id":"1",
"title":"Title",
"details":"details",
"benefit":"Welcome",
"expirationTimestamp":1549995900,
"notice":"some text",
}
]
Run Code Online (Sandbox Code Playgroud)
如果我做
given()
.spec(reqSpec).
when()
.get().
then()
.body("$", hasKey("id"));
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:
java.lang.AssertionError: 1 expectation failed.
JSON path $ doesn't match.
Expected: map containing ["id"->ANYTHING]
Actual: [{blabla=something, id=1, details=details, etc=etc}]
Run Code Online (Sandbox Code Playgroud)
请问,有人可以向我解释一下这应该如何工作吗?
json jsonpath rest-assured web-api-testing rest-assured-jsonpath