Home » Software Testing » Automation Testing » Selenium WebDriver Tutorial » Rerun failed Test cases instantly Using Junit..!!

Rerun failed Test cases instantly Using Junit..!!

We guys encounter sometime in need to rerun our failed test cases instantly, But if we wanna retry our test case twice/thrice etc then it is not achievable with Junit. But if we are following TestNG we can do that very fast.

But in Junit as well, we can achieve this using following way and can rerun failed test cases.

We can run this by defining a TestRule, This will give you the flexibility you need. A TestRule allows you to insert logic around the test, so you would implement the retry loop. For this you need to define a class in your framework :-

public class RetryTest {
public static class Retry implements TestRule {
private int retryCount;

public Retry(int retryCount) {
this.retryCount = retryCount;

public Statement apply(Statement base, Description description) {
return statement(base, description);

private Statement statement(final Statement base, final Description description) {
return new Statement() {
public void evaluate() throws Throwable {
Throwable caughtThrowable = null;

// implement retry logic here
for (int i = 0; i < retryCount; i++) {
try {
} catch (Throwable t) {
caughtThrowable = t;
System.err.println(description.getDisplayName() + “: run ” + (i+1) + ” failed”);
System.err.println(description.getDisplayName() + “: giving up after ” + retryCount + ” failures”);
throw caughtThrowable;

Now its very simple to rerun failed test cases with adding the rule before @Test:-

Say we want to run 3 retry for our test case

@Rule public Retry retry = new Retry(3);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top