Rerun failed tests is important in test automation projects. In this tutorial, I will explain to you how to repeat JUnit 5 tests when they are failed. Before going into this feature, let’s check JUnit 5’s default repeat test feature which is annotated by @RepeatedTest. It is very similar to TestNG’s “@Test(invocationCount = 5)” annotation. It just repeats the tests with a given repeat count.
First is first, you need to add the dependencies as shown below.
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.1.0</version> <scope>test</scope> </dependency>
Now, we are ready to go to write a simple test to verify JUnit 5 @RepeatedTest annotation.
Here is the sample code snippet.
@RepeatedTest(3) public void failTest () { assertTrue(false); }
Here is the result:
You can also give a name to your test with @RepeatTest annotation. The first one is LONG_DISPLAY_NAME enum is showed as follows:
@RepeatedTest(value=3, name=RepeatedTest.LONG_DISPLAY_NAME) public void failTest () { assertTrue(false); }
The result will be like this:
The other one is SHORT_DISPLAY_NAME but as you see it is a default behavior and no need to use this enum.
Also, you can use your custom names. As shown below:
@RepeatedTest(value=3, name=" My failing test {currentRepetition}/{totalRepetitions}") public void failTest () { assertTrue(false); }
You can also gather repetition info as follows:
@RepeatedTest(2) void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) { System.out.println("Repetition number:" + repetitionInfo.getCurrentRepetition()); assertEquals(2, repetitionInfo.getTotalRepetitions()); }
The output will be like this:
Repetition #1
Repetition #2
Honestly, this is a dummy retry annotation. It just retries your tests without checking their results, in order to add some logic and retry the tests when they are failing, you should add rerunner-jupiter dependency in your pom.xml. (Note: This feature is not working when you are using JUnit 5.3.1 parallel test execution feature.)
<dependency> <groupId>io.github.artsok</groupId> <artifactId>rerunner-jupiter</artifactId> <version>LATEST</version> </dependency>
Then you need to use @RepeatedIfExceptionsTest annotation to run your test when they are failed. This annotation has some extra features.
@RepeatedIfExceptionsTest(repeats = 3)
This annotation retries your test when it fails three times.
@RepeatedIfExceptionsTest(repeats = 5, exceptions = IOException.class)
This annotation reruns your test five times if the test fails. Set IOException.class that will be handled in test @throws IOException – error occurred.
@RepeatedIfExceptionsTest(repeats = 6, exceptions = IOException.class,
name = “Retry failed test. Attempt {currentRepetition} of {totalRepetitions}”)
This annotation repeats your test 6 times when it fails. You can also set a custom name. Set IOException.class that will be handled in the test. Set formatter for the test. Like behavior as at {@link org.junit.jupiter.api.RepeatedTest} @throws IOException – error occurred
@DisplayName(“Test Case Name”)
@RepeatedIfExceptionsTest(repeats = 100, minSuccess = 4)
This annotation repeats the test 100 times with minimum four consecutive successful run then stops the test execution, disables all remaining repeats.
Here are the basic usage and result of this Retry feature.
@RepeatedIfExceptionsTest(repeats = 3) public void failTest () { assertTrue(false); }
There is also another library available it is called as junit-pioneer but junit-reruner provides more features. You can try both of them and add your automation suites.
Junit-pioneer dependency is shown below:
<dependency> <groupId>org.junit-pioneer</groupId> <artifactId>junit-pioneer</artifactId> <version>0.2.2</version> <scope>test</scope> </dependency>
And its usage is as follows:
@RepeatFailedTest(3) public void failTest () { assertTrue(false); }
Thanks.
Onur Baskirt

Onur Baskirt is a Software Engineering Leader with international experience in world-class companies. Now, he is a Software Engineering Lead at Emirates Airlines in Dubai.
The last example with pioneer framework does not work. There is no such annotation with name: @RepeatFailedTest(3) available.
Also the rerunner-jupiter is extremely buggy. It creates huge mess in reports.
Hi Alex, I referred this link: https://github.com/junit-pioneer/junit-pioneer/commit/15ab0a6c97254335d877aa5587fc519ed8abf7f3#diff-94a60a248ed1fcd2fa7028a25ea6664e
I agree with you about JUnit re-runners immaturity. If you have a better solution for this operation, I would like to hear from you and add it to the article by referring your name. Kind Regards.
Thank you for your article, also junit-pioneer worked as expected for me whereas rerunner-jupiter didn’t (JUnit 5, jdk11).
Thank you for feedback.
If I need to rerun failed parametrized tests which are running in parallel will these approaches can do that? I also need that if at 1 test failed but on second run it passed, my build should be successful.
Hi Olena,
I did not try these for parallel test execution. You can give it a shot. Also you can check this: https://maven.apache.org/surefire/maven-surefire-plugin/examples/rerun-failing-tests.html