In JUnit 5, @RepeatedTest allows the same test logic to be executed multiple times. By default, JUnit executes all repetitions even if some of them fail. The failureThreshold attribute changes this behavior by instructing the engine to abort remaining repetitions once a specified number of failures is reached.
This capability is particularly useful for tests that validate stability, retry logic, or flaky integrations, where continuing execution after repeated failures provides no additional value and only increases execution time.
Example
package com.logicbig.example;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import static org.junit.jupiter.api.Assertions.fail;
public class RepeatedTestFailureThresholdTest {
@RepeatedTest(value = 5, failureThreshold = 2)
void repeatedTestWithFailureThreshold(RepetitionInfo repetitionInfo) {
int current = repetitionInfo.getCurrentRepetition();
int threshold = repetitionInfo.getFailureThreshold();
System.out.println("repetition=" + current + ", failureThreshold=" + threshold);
if (current == 2 || current == 4) {
fail("intentional failure");
}
}
}
Output$ mvn test -Dtest=RepeatedTestFailureThresholdTest [INFO] Scanning for projects... [INFO] [INFO] ----< com.logicbig.example:junit-5-repeated-test-failure-threshold >---- [INFO] Building junit-5-repeated-test-failure-threshold 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-repeated-test-failure-threshold --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-repeated-tests\junit-5-repeated-test-failure-threshold\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-repeated-test-failure-threshold --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-repeated-test-failure-threshold --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-repeated-tests\junit-5-repeated-test-failure-threshold\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-repeated-test-failure-threshold --- [INFO] Recompiling the module because of changed source code. [INFO] Compiling 1 source file with javac [debug target 25] to target\test-classes [INFO] [INFO] --- surefire:3.5.4:test (default-test) @ junit-5-repeated-test-failure-threshold --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- repetition=1, failureThreshold=2 repetition=2, failureThreshold=2 repetition=3, failureThreshold=2 repetition=4, failureThreshold=2 [INFO] +--com.logicbig.example.RepeatedTestFailureThresholdTest - 0.159 ss [INFO] | +-- [OK] repeatedTestWithFailureThreshold(RepetitionInfo) repetition 1 of 5 - 0.054 ss [INFO] | +-- [XX] repeatedTestWithFailureThreshold(RepetitionInfo) repetition 2 of 5 - 0.023 ss [INFO] | +-- [OK] repeatedTestWithFailureThreshold(RepetitionInfo) repetition 3 of 5 - 0.004 ss [INFO] | +-- [XX] repeatedTestWithFailureThreshold(RepetitionInfo) repetition 4 of 5 - 0.006 ss [INFO] | '-- [??] repeatedTestWithFailureThreshold(RepetitionInfo) repetition 5 of 5 (Failure threshold [2] exceeded) - 0 ss [INFO] [INFO] Results: [INFO] [ERROR] Failures: [ERROR] RepeatedTestFailureThresholdTest.repeatedTestWithFailureThreshold:19 intentional failure [ERROR] RepeatedTestFailureThresholdTest.repeatedTestWithFailureThreshold:19 intentional failure [INFO] [ERROR] Tests run: 5, Failures: 2, Errors: 0, Skipped: 1 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.114 s [INFO] Finished at: 2025-12-27T14:57:11+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.5.4:test (default-test) on project junit-5-repeated-test-failure-threshold: There are test failures. [ERROR] [ERROR] See D:\example-projects\junit-5\junit-5-repeated-tests\junit-5-repeated-test-failure-threshold\target\surefire-reports for the individual test results. [ERROR] See dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream. [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
In this example, the test is configured to run five times, but execution stops as soon as the second failure occurs. The output confirms that JUnit does not execute remaining repetitions once the failure threshold is exceeded, which demonstrates how failureThreshold provides deterministic early termination for repeated tests.
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.10.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 5.10.0
- 5.10.1
- 5.10.2
- 5.10.3
- 5.10.4
- 5.10.5
- 5.11.0
- 5.11.1
- 5.11.2
- 5.11.3
- 5.11.4
- 5.12.0
- 5.12.1
- 5.12.2
- 5.13.0
- 5.13.1
- 5.13.2
- 5.13.3
- 5.13.4
- 5.14.0
- 5.14.1
- 6.0.0
- 6.0.1
Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|