Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7186529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:39:00+00:00 2026-05-28T18:39:00+00:00

I am using JUnit 4.10 for running test suites, and I have implemented a

  • 0

I am using JUnit 4.10 for running test suites, and I have implemented a “retry failed test” rule following Matthew Farwell’s awesome notes in the How to Re-run failed JUnit tests immediately? post. I created a class “RetryTestRule” with the following code:

public class RetryTestRule implements TestRule {

  private final int retryCount;

  public RetryTestRule(int retryCount) {
    this.retryCount = retryCount;
  }

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

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

        // retry logic
        for (int i = 0; i < retryCount; i++) {
          try {
            base.evaluate();
            return;
          } 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;
      }
    };
  }
}

When using this as a rule inside a test case it works perfectly, but it seems not optimal to use the @Rule notation in every test case of a suite instead of a single notation in the Suite definition, so after checking a bit I tried the new @ClassRule notation in my Suite class:

@RunWith(Suite.class)
@SuiteClasses({
  UserRegistrationTest.class,
  WebLoginTest.class
})
public class UserSuite {    
  @ClassRule
  public static RetryTestRule retry = new RetryTestRule(2);
}

Problem is this does not work as expected: failed tests are not being retried. Does anyone have tried this and knows a solution? Help is much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:39:01+00:00Added an answer on May 28, 2026 at 6:39 pm

    @ClassRules are executed once per class, not once per method. To have something executed once per method, you need to use @Rule like you are doing, or follow the answer for How to define JUnit method rule in a suite?.

    To reuse your existing rule, you can add the rule to the list of rules to run, using the RunRules class as follows:

    public class MyRunner extends BlockJUnit4ClassRunner {
        public MyRunner(Class<?> klass) throws InitializationError {
            super(klass);
        }
    
        @Override
        protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
            Description description= describeChild(method);
            if (method.getAnnotation(Ignore.class) != null) {
                notifier.fireTestIgnored(description);
            } else {
                RunRules runRules = new RunRules(methodBlock(method), Arrays.asList(new TestRule[]{new RetryTestRule(3)}), description);
                runLeaf(runRules, description, notifier);
            }
        }
    }
    

    This is using the example from the above answer. You could probably combine the two answers as well for more fine grained control, creating a RetryTestRule if there were an annotation on your test for example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using data-driven test suites running JUnit 3 based on Rainsberger's JUnit Recipes
I'm using JUnit 4.4 and Maven and I have a large number of long-running
I am running JUnit tests using in memory HSQLDB. Let's say I have a
Running the Java Compiler from a JUnit test is pretty simple using the Java
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
We are using Junit + Selenium to webtest our webpage. But we have run
We have a lot of integration tests written using JUnit 3 , though we're
I have two tests to check the expected exception throw. I am using Junit
I have a test that exercises a custom Swing component using java.awt.Robot. I'd like
I am running a Junit test case on my eclipse application that was built

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.