RestAssured: How to check the length of json array response?

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


Solved! I have solved it this way:

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

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


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));