11

I have test(!) code of RESTAssured, that checks that REST endpoint returns me 0 as status code;

     given()
        .contentType(CONTENT_TYPE_APPLICATION_JSON)
    .when()
        .get(getRestOperationPath())
    .then()
        .statusCode(STATUS_CODE_OK); 

But now it is possible that it also can supply code 404, that is considered valid output. I need my test to check that status code is one of the two, but I cannot wrap my head on how to do actually do it. Can you point me as to how I can do it, or if it is impossible?

upd:

.get(getRestOperationPath()) returns Response -> you can get status code and compare it. closed.

Andrii Plotnikov
  • 3,022
  • 4
  • 17
  • 37

3 Answers3

19

You could also do this using the Hamcrest matchers, without extracting the response into a separate variable.

You can use a combination of anyOf() and is()

import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.is;

...

given()
    .contentType(CONTENT_TYPE_APPLICATION_JSON)
.when()
    .get(getRestOperationPath())
.then()
    .statusCode(anyOf(is(STATUS_CODE_OK),is(STATUS_CODE_NOT_FOUND))); 
heisa
  • 834
  • 1
  • 9
  • 17
  • had to close the question to get right answer :D but I can't use hamcrest, so will he to leave with my version – Andrii Plotnikov Jan 27 '17 at 09:41
  • Both are equally valid options. Just depending on the situation. No need to include Hamcrest specifically for this, but if you're including it anyway... – heisa Jan 27 '17 at 09:42
1

.get(getRestOperationPath()) returns Response. You can .getStatusCode() and compare it. Closed.

Answer just to close question.

Grzegorz Górkiewicz
  • 4,496
  • 4
  • 22
  • 38
Andrii Plotnikov
  • 3,022
  • 4
  • 17
  • 37
0

if you want to check for more than one error code within the body, you can alternatively do

import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.is;

response
   .jsonPath()
   .getString("errorCode")
   .equals(
      anyOf(is("E_001"),
            is("E_002"),
            is("E_003")));
Jonathan
  • 595
  • 5
  • 10