Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry only on specific exception #356

Open
arturmkr opened this issue Dec 4, 2024 · 0 comments
Open

Retry only on specific exception #356

arturmkr opened this issue Dec 4, 2024 · 0 comments

Comments

@arturmkr
Copy link

arturmkr commented Dec 4, 2024

Is it possible to configure a retry mechanism in tests to run only on specific exceptions?

For example, in the following code, I want the test to retry only if the UnexpectedStatusCodeException is thrown. If any other assertions (like assertEquals) fail, the test should not retry.

code:

public class MyDemoTest {
    @Test
    void sendDataTest() throws UnexpectedStatusCodeException {
        RestAssured.baseURI = "https://petstore.swagger.io/";

        Response response = given()
                .when()
                .get("v2/pet/" + 1);

        checkStatusCode(response, 200);

        String name = response.jsonPath().getString("name");
        assertEquals("dog", name);
    }
    
    static void checkStatusCode(Response response, int expectedStatusCode) throws UnexpectedStatusCodeException {
        int statusCode = response.getStatusCode();
        if (statusCode != expectedStatusCode) {
            throw new UnexpectedStatusCodeException("Unexpected status code: " + statusCode);
        }
    }

    static class UnexpectedStatusCodeException extends Exception {
        public UnexpectedStatusCodeException(String message) {
            super(message);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant