How can I check a json array using the rest of the information?

I have a JSON response:

["alice", "jason", "steve", "alex"]

      

then when you remain confident to check:

when().
       get("/names").
then().
       body(containsInAnyOrder("alice","jason","steve","alex"));

      

It doesn't work 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 doesn't work.

How can I validate a simple JSON array in the response?

+3


source to share


2 answers


After reading the doc I found the answer:



https://github.com/rest-assured/rest-assured/wiki/Usage#anonymous-json-root-validation

+3


source


To save any click, you must provide a redundant string to call the body method:

when().
   get("/names").
then().
   body("", hasItems("alice","jason","steve","alex"));

      



Also, even if you only have one element in your array, you still have to use hasItems

instead of hasItem

. For example:

when().
   get("/names").
then().
   body("", hasItems("alice"));

      

0


source







All Articles