7

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

Michal
  • 563
  • 3
  • 14
  • 24
  • If you want to retry something loop is your only way. And what's wrong with simple "not clever" solution? – Guy Jan 21 '16 at 09:26
  • 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") ... – Michal Jan 21 '16 at 09:38

2 Answers2

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.

sagaris
  • 31
  • 1
  • 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") ? – Erran Morad Apr 06 '19 at 20:37
  • 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++; }' – sagaris Apr 11 '19 at 11:42
0

You can also use SpringRetry library and RetryTemplate class.

Naeem A. Malik
  • 995
  • 4
  • 19