我正在与Rest-Assureed一起进行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"。
谢谢。