Rest Assured - retry request if failed
Asked Answered
L

5

8

Example Test:

@Test
public void shouldGetRoutesList() {
    Response response =
            given()
                    .headers("RequestId", 10)
                    .headers("Authorization", accessToken)
                    .contentType(ContentType.JSON).
            expect()
                    .statusCode(HttpURLConnection.HTTP_OK).
            when()
                    .get("address");
    String responseBody = response.getBody().asString();
    System.out.println(responseBody);
    logger.info("Log message");
}

Thing is that sometimes response from service equals 500 error. It's because of application error and it's application fault so I would like to add temp. workaround to retry .get if service returns 500. I was thinking about if or do-while but I know that it's not very clever way. Anyone could advice some solution ?

In another words - I want to retry whole test (or just .get) if statusCode=!HTTP_OK

Lime answered 21/1, 2016 at 8:53 Comment(2)
If you want to retry something loop is your only way. And what's wrong with simple "not clever" solution?Sliding
I could remove expect() to not fail test because of statusCode=500 and in IF statement check that this code is/or is not visible. But another issue is that responseBody do not return status code as a String so I can't just do something like responseBody.contains("500") ...Lime
S
0

If you are using TestNG, than you can implement your own retryAnalyzer: http://toolsqa.com/selenium-webdriver/retry-failed-tests-testng/

In case of other frameworks the "not very clever" solutions are your answers, catch the exception and try again until exit criteria matches.

Unfortunately RestAssured has not mechanism to retry.

Syngamy answered 13/2, 2018 at 15:3 Comment(2)
A simple loop could solve the problem. But, what if I want to keep retrying until certain conditions are achieved. Ex. myRequestObject.doRetry(3times, with 5sec pause, until responseCode=200 AND responseBody.message.status ="success") ?Ryeland
Than use these conditions as exit crieria in a while loop for instance. 'while(numOfAttempt <= 3 && respCode != 200 && !message.equal("success")) { do the request... save the respCode and message; numOfAttemp++; }'Syngamy
S
0

You can also use SpringRetry library and RetryTemplate class.

Serranid answered 20/3, 2022 at 13:39 Comment(0)
K
0

Check out the Dev.Failsafe library. They have a RetryPolicy class that works great with RestAssured. This video covers it well: https://www.youtube.com/watch?v=MwY4VP3lQ7I

Kittiekittiwake answered 15/9, 2023 at 1:50 Comment(0)
T
0

To enable retry mechanism make use of awaitility library.

This blog has good examples.

@Test
public void waitTest() throws Exception {
    Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> this.getStatusCode() == 200)
}

public int getStatusCode() {
    return given()
        .contentType(ContentType.JSON)
        .get('testapiurl')
        .then()
        .extract()
        .statusCode();
}
Taciturnity answered 17/10, 2023 at 17:44 Comment(0)
S
0

If all you're required is just to get some particular status code and it isn't in the testing scope, use plain old do/while like:

int counter = 0;
int statusCode = 200;
Response response;
do {
  if (counter > 0) {
    try {
      log.severe(String.format("the previous request failed with the '%s' status code - holding runtime for 1 second and retrying", statusCode));
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
  }
  response = given()
      .spec(getSomeSpec())
      .queryParams(queryParams)
      .get();
  statusCode = response.statusCode();
  counter += 1;
} while (statusCode != 200 && counter <= 2);
Stupid answered 21/10, 2023 at 21:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.