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

Héctor picture Héctor · Apr 7, 2016 · Viewed 26.9k times · Source

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

Answer

Héctor picture Héctor · Apr 7, 2016

Solved! I have solved it this way:

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