Some tests, e.g. those depending on external systems, may fail through no fault of the code under test. Such tests make a suite fragile and it makes sense to try and avoid them, but if that’s infeasible, it may help to retry a number of times before eventually assuming actual failure. @RepeatFailedTest provides that functionality.

Basic Use

@RepeatFailedTest(n) is used instead of @Test or other such annotations (e.g. @RepeatedTest). The attribute n specifies how often the test is executed before giving up.

@RepeatFailedTest(3)
void failsNever() {
    // passing test code
}

The test failsNever is executed once (which succeeds) and marked as passed.

@RepeatFailedTest(3)
void failsOnlyOnFirstInvocation() {
    // test code that fails on first execution
    // but passes on the second
}

The test failsOnlyOnFirstInvocation is executed once (which fails) and then once more (which succeeds). To allow the entire test suite to pass, the first execution is marked as ignored/aborted, which includes the underlying exception - the second is of course marked as passing.

@RepeatFailedTest(3)
void failsAlways() {
    // test code that always fails
}

The test failsAlways is executed three times (all of which fail). The first two executions are marked as ignored/aborted and the last as failed - each contains the underlying exception.