5

I would like to assert if the status code returned is any of the 2xx series i.e 200 or 201 or 204. I don't want to do it the old fashioned way e.g.

if(response.statusCode() == 200 || response.statusCode() == 201 || 
   response.statusCode() == 204) {
     
     \\...
  }

Is there any better way to do?

dipindashoff
  • 103
  • 1
  • 6

2 Answers2

2

import io.restassured.internal.http.Status;

Assert.assertTrue(Status.SUCCESS.matches(response.statusCode())); // true for family of 
2xx eg 200 or 201

Reference: Mapping of HTTP response codes to a constant 'success' or 'failure' value.

dipindashoff
  • 103
  • 1
  • 6
0

You can use a CustomTypeSafeMatcher

 given()
.
.
.
.statusCode(new CustomTypeSafeMatcher<>("2xx StatusCode") {
   @Override
   protected boolean matchesSafely(Integer item) {
         return Status.SUCCESS.matches(item);
   }
})
Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136