使用 REST Assured,我如何检查一个字段是否存在于 json 对象类型的响应数组中?
Using REST Assured, how can I check if a field is present or not in an array of json objects type of response?
我需要验证如下所示的响应是否包含一些字段。我对字段值不感兴趣 - 只是键存在。
例如,我想检查密钥 "id" 是否存在于此类响应中。我将如何做到这一点?
[
{
"id":"1",
"title":"Title",
"details":"details",
"benefit":"Welcome",
"expirationTimestamp":1549995900,
"notice":"some text",
}
]
如果我这样做
given()
.spec(reqSpec).
when()
.get().
then()
.body("$", hasKey("id"));
我收到这样的错误:
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}]
有人能给我解释一下这是怎么回事吗?
我需要验证如下所示的响应是否包含一些字段。我对字段值不感兴趣 - 只是键存在。 例如,我想检查密钥 "id" 是否存在于此类响应中。我将如何做到这一点?
[
{
"id":"1",
"title":"Title",
"details":"details",
"benefit":"Welcome",
"expirationTimestamp":1549995900,
"notice":"some text",
}
]
如果我这样做
given()
.spec(reqSpec).
when()
.get().
then()
.body("$", hasKey("id"));
我收到这样的错误:
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}]
有人能给我解释一下这是怎么回事吗?