total / count items from list json response

15,041

Solution 1

I got answer to my question.

here is the code for rest assured

expect().body("final_list.findall.size()",equalTo(2)).when().get("/list.json);

thanks

Solution 2

if you expect final_list to have 2 elements, you can use the hasSize() matcher:

expect().body("final_list", hasSize(2)).when().get("/list.json);

I found this in the list of matchers from the Matchers class Java doc:

http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#hasSize(org.hamcrest.Matcher)

Solution 3

Yo can do the following:

jp = new JsonPath(response.asString());
String numFromsResponse = jp.get("final list.id.size()").toString();
Share:
15,041

Related videos on Youtube

dileepvarma
Author by

dileepvarma

Updated on June 04, 2022

Comments

  • dileepvarma
    dileepvarma almost 2 years

    im trying to get count / total number of records from list api using Restassured.

    Please share thoughts on how can we get it.

    ex:

    {
        "final list": [
            {
                "id": 123,
                "name": "sachin"
            },
            {
                "id": 234,
                "name": "sourav"
            }
        ]
    }
    

    I am expecting total as 2 here. please suggest.

    • Hussain Akhtar Wahid 'Ghouri'
      Hussain Akhtar Wahid 'Ghouri' almost 11 years
      jsonObject.length , how about that ???
    • dileepvarma
      dileepvarma almost 11 years
      pls tell me in restassured code
  • dileepvarma
    dileepvarma almost 11 years
    hi anand.. im verifying response data using junit and i wanted to test number of records in the list json response
  • Kai Carver
    Kai Carver about 10 years
    a slightly simpler solution is: expect().body("final_list", hasSize(2)).when().get("/list.json);