24

How can I check with rest-assured (2.4.0) if the response json is an empty list?

Given the response [] (with header content-type=application/json) I tried:

.body(Matchers.emptyArray()) // expected: an empty array, actual: []
.body("/", Matchers.emptyArray()) // invalid expression /
.body(".", Matchers.emptyArray()) // invalid expression .
atamanroman
  • 11,607
  • 7
  • 57
  • 81
  • If it could help, this passes the matcher : `Object array[] = new Object[0];` `new MatcherAssert().assertThat(array, Matchers.emptyArray());` – romfret May 26 '15 at 14:15

2 Answers2

47

The problem is (probably) that REST Assured returns a List and not an array (and Hamcrest differentiate between the two). You can do:

.body("", Matchers.hasSize(0))

or

.body("$", Matchers.hasSize(0))

or

.body("isEmpty()", Matchers.is(true))
Johan
  • 37,479
  • 32
  • 149
  • 237
3

Inspired by what @Johan said I tried this and I think it tells more to reader than other suggestions.

.body("", equalTo(Collections.emptyList()))
kazimierz
  • 31
  • 1