简体   繁体   English

Rest Assured - 如果失败重试请求

[英]Rest Assured - retry request if failed

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.事情是有时来自服务的响应等于 500 错误。 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.如果服务返回 500,则重试.get的解决方法。我在考虑ifdo-while但我知道这不是很聪明的方法。 Anyone could advice some solution?任何人都可以建议一些解决方案吗?

In another words - I want to retry whole test (or just.get) if statusCode=!HTTP_OK换句话说 - 如果 statusCode=!HTTP_OK,我想重试整个测试(或 just.get)

If you are using TestNG, than you can implement your own retryAnalyzer: http://toolsqa.com/selenium-webdriver/retry-failed-tests-testng/ 如果您使用的是TestNG,那么您可以实现自己的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. 不幸的是,RestAssured没有机制重试。

You can also use SpringRetry library and RetryTemplate class.也可以使用SpringRetry库和RetryTemplate class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM