Close

JUnit 5 - Introduction to Repeated Tests

[Last Updated: Dec 25, 2025]

What is @RepeatedTest and When to Use It?

The @RepeatedTest annotation in JUnit Jupiter allows you to execute a test method multiple times. It's particularly useful for verifying behavior that might be sensitive to non-deterministic factors or for simple load testing scenarios. A non-deterministic test, sometimes called a "flaky test," is a test that can produce inconsistent outcomes—passing on some runs and failing on others—despite no changes to the codebase or test environment.
Unlike regular tests, repeated tests inherently focus on running the same test logic multiple times.

Definition of RepeatedTest

Version: 6.0.0
Since : 5.4.0
 package org.junit.jupiter.api;
 @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @API(status = STABLE, since = "5.0")
 @TestTemplate
 public @interface RepeatedTest {
     int value(); 1
     String name() default SHORT_DISPLAY_NAME; 2
     @API(status = MAINTAINED, since = "5.13.3")
     int failureThreshold() default Integer.MAX_VALUE; 3
 }
1The number of repetitions.
2The display name for each repetition of the repeated test.
3Configures the number of failures after which remaining repetitions will be automatically skipped. (Since 5.10)

Basic Syntax

To create a repeated test, you replace @Test with @RepeatedTest and specify the number of repetitions. Each repetition will be treated as a distinct test execution by the JUnit test runner.

Difference between @Test and @RepeatedTest

The primary difference lies in their intent. @Test is for single, independent test executions, ensuring a specific behavior works once. @RepeatedTest, however, explicitly runs the same test logic multiple times. This is useful for observing consistency or exposing intermittent issues.

Key Concepts

  • Deterministic Repetition: @RepeatedTest provides a controlled and predictable way to run a test multiple times with the exact same setup and assertions for each run.
  • Why Repetition is not the same as Parameterization: While both involve running a test multiple times, @RepeatedTest runs the same test with the same inputs repeatedly. Parameterized tests (@ParameterizedTest) run the same test logic with different sets of input data for each execution. Repetition focuses on consistency, while parameterization focuses on input variations.

In the next tutorials we will see the examples of @RepeatedTest.

See Also