36

I have an endpoint that returns a JSON like:

[
  {"id" : 4, "name" : "Name4"},
  {"id" : 5, "name" : "Name5"}
]

and a DTO class:

public class FooDto {
    public int id;
    public String name;
}

Now, I'm testing the length of the returned json array this way:

@Test
public void test() {
    FooDto[] foos = RestAssured.get("/foos").as(FooDto[].class);
    assertThat(foos.length, is(2));
}

But, is there any way to do it without cast to FooDto array? Something like this:

@Test
public void test() {
    RestAssured.get("/foos").then().assertThat()
      .length(2);
}
Héctor
  • 24,444
  • 35
  • 132
  • 243

8 Answers8

78

Solved! I have solved it this way:

@Test
public void test() {
    RestAssured.get("/foos").then().assertThat()
      .body("size()", is(2));
}
Héctor
  • 24,444
  • 35
  • 132
  • 243
  • Hi, How to pass size restriction, when doing non-BDD style rest-assured. viz- public Response putRequest() { httpRequest.header("Content-Type",ContentType.JSON); httpRequest.header("Accept",ContentType.JSON); httpRequest.header("Authorization", System.getProperty("apiKey")); return httpRequest.request(Method.PUT); } – user2451016 Mar 14 '22 at 20:22
14

You can simply call size() in your body path:

Like:

given()
            .accept(ContentType.JSON)
            .contentType(ContentType.JSON)
            .auth().principal(createPrincipal())
            .when()
            .get("/api/foo")
            .then()
            .statusCode(OK.value())
            .body("bar.size()", is(10))
            .body("dar.size()", is(10));
Michel
  • 9,220
  • 13
  • 44
  • 59
9

There are ways. I solved with the below

@Test
public void test() {
 ValidatableResponse response = given().when().get("/foos").then();
 response.statusCode(200);
 assertThat(response.extract().jsonPath().getList("$").size(), equalTo(2));
}

using restassured 3.0.0

arulraj.net
  • 4,579
  • 3
  • 36
  • 37
5

I solved similar task wit GPath.

Response response = requestSpec
                .when()
                .get("/new_lesson")
                .then()
                .spec(responseSpec).extract().response();

Now I can extract response body as String and use built-in GPath capabilities

String responseBodyString = response.getBody().asString();

assertThat(from(responseBodyString).getList("$").size()).isEqualTo(YOUR_EXPECTED_SIZE_VALUE);
assertThat(from(responseBodyString).getList("findAll { it.name == 'Name4' }").size()).isEqualTo(YOUR_EXPECTED_SUB_SIZE_VALUE);

For full example see http://olyv-qa.blogspot.com/2017/07/restassured-short-example.html

olyv
  • 3,699
  • 5
  • 37
  • 67
5

This is what you need to do in order to get size.

Response response = 
given()
.header("Accept","application/json")
.when()
.get("/api/nameofresource")
.then()
.extract()
.response();

once you have the response, you can do the following to get the size.

int size = response.jsonPath().getList("id").size();

hope this helps. :)

roy
  • 81
  • 1
  • 7
4

@Héctor

[   
   {"id" : 4, "name" : "Name4"}, 
   {"id" : 5, "name" : "Name5"}
]

It was really unlucky example. Here you have matrix [2,2].

If you create something like this:

[   
   {"id" : 4, "name" : "Name4"}, 
   {"id" : 5, "name" : "Name5"}, 
   {"id" : 6, "name" : "Name6"} 
]

Now you still get passed test:

@Test
public void test() {
    RestAssured.get("/foos").then().assertThat()
      .body("size()", is(2));
}

Consider about whether it was intentional. body("size()",is(2)); Have checking length of one node instead of number of records in response.

Liniel
  • 719
  • 1
  • 6
  • 15
2

Also possible option is hasSize() matcher along with "." path:

import static org.hamcrest.Matchers.hasSize;

...

@Test
public void test() {
    RestAssured.get("/foos").then().assertThat()
      .body(".", hasSize(2));
}
Rib47
  • 2,336
  • 2
  • 14
  • 15
0

Try this :)

   Response response = RestAssured
   .given()    
        .header("Content-Type", "application/json")    
   .when()    
        .get("api/v1/blablabla/");    
        
    int responseLength = 
    response.body().jsonPath().getList("$").size();      
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Cristik Feb 05 '22 at 12:23