使用 REST Assured,如何检查响应的 json 对象类型数组中是否存在某个字段?

2024-01-03

我需要验证像下面这样的响应是否包含一些字段。我对字段值不感兴趣 - 只是对键存在感兴趣。 例如,我想检查这种类型的响应中是否存在键“id”。我将如何实现这一目标?

[  
   {  
      "id":"1",
      "title":"Title",
      "details":"details",
      "benefit":"Welcome",
      "expirationTimestamp":1549995900,
      "notice":"some text",     
   }
]

If I do

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}]

请问,有人可以向我解释一下这应该如何工作吗?


尝试这个:

given()
  .spec(reqSpec).
when()
  .get().
then()
  .body("[0]", hasKey("id"));

Groovy GPath 用于放心。你可以看看他们的指南here http://groovy-lang.org/processing-xml.html#_gpath

还有一个很好的教程here https://james-willett.com/2017/05/rest-assured-gpath-json/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 REST Assured,如何检查响应的 json 对象类型数组中是否存在某个字段? 的相关文章

随机推荐