Erike Dunbar
Erike Dunbar

Reputation: 3

failed JUnit test reruns once and stops

My failed Junit test reruns once and stops but i am looking to have it run 3 times. I guess my annotation is screwed in some way. Pls what is the proper format for annotation. Note: I will also appreciate info about correct annotation for testing SuiteClasses. Below is my annotation

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

any help will be appreciated

public class HostTest {

/**
 * Test method for {@link com.ibm.ws.ui.collective.internal.rest.resource.Host}.
 */
public class RetryTest {
    /**
     * @param i
     */
    public RetryTest(int i) {
        // TODO Auto-generated constructor stub
    }

    public class Retry extends TestRule {
        private final int retryCount;

        public Retry(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;

                    // implement retry logic here
                    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;
                }
            };
        }
    }


}
 @Test
public void sanityCheck_withHostPathsNull() {
    Host h = new Host("myHost");
    Map<String, Object> mapFromController = new HashMap<String, Object>();
    h.setHostPaths(mapFromController);
    assertFalse("it is not empty", h.getHostPaths().isEmpty());

}

}

Upvotes: 0

Views: 87

Answers (1)

dkatzel
dkatzel

Reputation: 31648

Are you sure the failed tests were only running once? You might want to put a counter or print statement to confirm.

I'm not sure what you thought the Retry Rule would do but this code:

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");
       }
 }

means that a test is only retried if it throws anything (meaning the test failed). Therefore, Retry(3) means to run each test and if any fail, then try running the failed tests upto 2 more times to see if it eventually passes. If after the final retry, if it still fails, then that final failure message is rethrown which will fail the test.

If you instead wanted to retry the tests over and over again even if the test passes, then you should get rid of that return statement inside the for loop. But then you have to be careful that you only rethrow the throwable at the end if it's not null. (meaning don't throw if the test passed the last time)

To make the test run over and over no matter if it passes:

            @Override
            public void evaluate() throws Throwable {
                Throwable caughtThrowable = null;

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

Upvotes: 1

Related Questions