How to verify json array using rest assured?

时间:2017-04-10 01:43:48

标签: 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?

1 个答案:

答案 0 :(得分:2)

要保存任何点击,您必须为body方法调用提供一个冗余字符串:

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

此外,即使您的阵列中只有一个项目,您仍然必须使用hasItems而不是hasItem。例如:

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