Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this with a <a href="http://junit-team.github.io/junit/javadoc/latest/org/junit/rules/TestRule.html" rel="noreferrer">TestRule</a>. 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:</p> <pre><code>public class RetryTest { public 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() { @Override public void evaluate() throws Throwable { Throwable caughtThrowable = null; // implement retry logic here for (int i = 0; i &lt; 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; } }; } } @Rule public Retry retry = new Retry(3); @Test public void test1() { } @Test public void test2() { Object o = null; o.equals("foo"); } } </code></pre> <p>The heart of a <code>TestRule</code> is the <code>base.evaluate()</code>, which calls your test method. So around this call you put a retry loop. If an exception is thrown in your test method (an assertion failure is actually an <code>AssertionError</code>), then the test has failed, and you'll retry.</p> <p>There is one other thing that may be of use. You may only want to apply this retry logic to a set of tests, in which case you can add into the Retry class above a test for a particular annotation on the method. <code>Description</code> contains a list of annotations for the method. For more information about this, see my answer to <a href="https://stackoverflow.com/questions/8233883/how-to-run-some-code-before-each-junit-test-method-individually-without-using/8234076#8234076">How to run some code before each JUnit @Test method individually, without using @RunWith nor AOP?</a>.</p> <h2>Using a custom TestRunner</h2> <p>This is the suggestion of CKuck, you can define your own Runner. You need to extend <a href="http://junit.sourceforge.net/javadoc/org/junit/runners/BlockJUnit4ClassRunner.html" rel="noreferrer">BlockJUnit4ClassRunner</a> and override runChild(). For more information see my answer to <a href="https://stackoverflow.com/questions/7639353/how-to-define-junit-method-rule-in-a-suite/7653558#7653558">How to define JUnit method rule in a suite?</a>. This answer details how to define how to run code for every method in a Suite, for which you have to define your own Runner.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload