简体   繁体   中英

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

I am working on REST API test automation with Rest-Assured. For one API I am getting an array like below in response. From that data array I need to check any of the array item has any property "requestRefNo" with value: "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": [

        ]
    }
}

I have tried like this:

.assertThat().statusCode(200).body("data.requestRefNo", IsArrayContaining.hasItemInArray("Sss/12345637/58"))))

But it is giving the below error:

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]

Can anyone give me any idea?

Thanks, Surodip

Got a very simple answer, missed earlier:

...
.body("data.requestRefNo", Matchers.hasItem("Sss/12345637/58"))
                                        .extract().response();

"data.requestRefNo" will return the array of all requestRefNo in the response array like [Sss/12345637/58, Sss/12345637/88] and Matchers.hasItem will check if the value "Sss/12345637/58" exists in that.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM