Let’s assume you have several JUnit tests in a test class. That class contains some tests, part of them are very stable but others are not.
To implemplement the JUnit retry logic for failed tests we have to create an annotation @Retry and implement TestRule interface (RetryRule).
@Retry annotation allows us to mark the JUnit tests that we want to apply the retry logic to.
Junit Retry Rule
RetryRule actually implements the retry logic in JUnit.
Annotate JUnit tests with @Retry
Now you need to define how many times we should run tests if they failed and mark tests that we want to use the retry logic for. In this example we set the number of retries to 2: RetryRule retryRule = new RetryRule(2)