Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify json array using rest assured?

Tags:

rest-assured

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?

like image 305
Jakim Avatar asked Apr 10 '17 01:04

Jakim


People also ask

How do you validate an entire JSON response in Rest assured?

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.

How do I read a JSON file in Rest assured?

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.


1 Answers

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"));
like image 164
Clarkey Avatar answered Sep 22 '22 07:09

Clarkey