Assert that response body is empty list with rest-assured

atamanroman picture atamanroman · May 26, 2015 · Viewed 23k times · Source

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 .

Answer

Johan picture Johan · May 26, 2015

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