Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert that response body is empty list with rest-assured

Tags:

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 . 
like image 898
atamanroman Avatar asked May 26 '15 13:05

atamanroman


People also ask

How do you assert an empty JSON response?

If you want to check if your response is not empty try : if ( json. length == 0 ) { console. log("NO DATA!") }

How do you assert an empty list in Junit?

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.

How do you validate a response body in Rest assured?

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.

How do you assert a Rest assured 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.


1 Answers

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)) 
like image 189
Johan Avatar answered Sep 17 '22 11:09

Johan