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 .
If you want to check if your response is not empty try : if ( json. length == 0 ) { console. log("NO DATA!") }
assertThat(myList, is(empty())); assertThat(myList, is(not(empty()))); You can add is as a static import to your IDE as I know that eclipse and IntelliJ is struggling with suggesting it even when it is on the classpath.
We can verify the JSON response body using Assertions in Rest Assured. This is done with the help of the Hamcrest Assertion. It uses the Matcher class for Assertion. We shall send a GET request via Postman on a mock API, observe the Response.
We can use Assertion in response in Rest Assured. To obtain the Response we need to use the methods - Response. body or Response. getBody.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With