I have a JSON response:
["alice", "jason", "steve", "alex"]
then when use rest assured to test:
when().
get("/names").
then().
body(containsInAnyOrder("alice","jason","steve","alex"));
This is not working as I expected, it gives an error:
Expected: iterable over ["alice", "jason", "steve", "alex"] in any order
Actual: ["alice", "jason", "steve", "alex"]
Also tried with:
when().
get("/names").
then().
body(hasItems("alice","jason","steve","alex"));
also not working.
How can I verify a simple JSON array in the response?
We can verify the JSON response body using Assertions in Rest Assured. This is done with the help of the Hamcrest Assertion. It uses the Matcher class for Assertion. We shall send a GET request via Postman on a mock API, observe the Response.
We can handle static JSON in Rest Assured. This can be done by storing the entire JSON request in an external file. First, the contents of the file should be converted to String. Then we should read the file content and convert it to Byte data type.
To save any clicking, you have to supply a redundant string to the body method call:
when().
get("/names").
then().
body("", hasItems("alice","jason","steve","alex"));
Additionally, even if you only have one item in your array, you still have to use hasItems
rather than hasItem
. For example:
when().
get("/names").
then().
body("", hasItems("alice"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With