![](/img/trans.png)
[英]Using REST assured with Java Hamcrest to check if Array contains item with multiple key value pairs where the values have a different type
[英]Hamcrest matcher to check if any of the element in response json array has a property value same as a specific value in Rest Assured
我正在使用 Rest-Assured 进行 REST API 测试自动化。 对于一个 API,我得到了一个如下所示的数组作为响应。 从该数据数组中,我需要检查任何数组项是否具有任何属性“requestRefNo”,其值为:“Sss/12345637/58”
{
"data": [
{
"requestRefNo": "Sss/12345637/88",
"requestRefType": "AST",
"requestedByCode": "OWR",
"requestedByDesc": "Asset Owner",
"requestedDate": "12/06/2016",
"requestTypeRefNo": "Sss/12345637/SWT/73"
},
{
"requestRefNo": "Sss/12345637/58",
"requestRefType": "AST",
"requestedByCode": "OWR",
"requestedByDesc": "Asset Owner",
"requestedDate": "10/06/2016",
"requestTypeRefNo": "Sss/12345637/SWT/43"
},
....
],
"links": {
"linkDetails": [
],
"empty": true
},
"errors": {
"empty": true,
"errorDetails": [
]
}
}
我试过这样:
.assertThat().statusCode(200).body("data.requestRefNo", IsArrayContaining.hasItemInArray("Sss/12345637/58"))))
但它给出了以下错误:
java.lang.AssertionError: 1 expectation failed.
JSON path data.requestRefNo doesn't match.
Expected: an array containing "Sss/12345637/58"
Actual: [Sss/12345637/58, Sss/12345637/88]
任何人都可以给我任何想法吗?
谢谢,苏洛迪普
得到了一个非常简单的答案,之前错过了:
...
.body("data.requestRefNo", Matchers.hasItem("Sss/12345637/58"))
.extract().response();
"data.requestRefNo" 将返回响应数组中所有 requestRefNo 的数组,如 [Sss/12345637/58, Sss/12345637/88] 和 Matchers.hasItem 将检查值 "Sss/12345637/58" 是否存在。
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.