0
0
JunitHow-ToBeginner ยท 3 min read

How to Use @RepeatedTest in JUnit for Repeated Test Execution

Use the @RepeatedTest annotation in JUnit to run a test method multiple times automatically. Specify the number of repetitions as a parameter, like @RepeatedTest(5) to run the test 5 times. This helps verify consistent behavior across repeated executions.
๐Ÿ“

Syntax

The @RepeatedTest annotation is used above a test method to run it multiple times. You provide the number of repetitions as an integer parameter. Optionally, you can add a name parameter to customize the display name for each repetition.

  • @RepeatedTest(value): Runs the test value times.
  • name (optional): Customizes the display name for each repetition using placeholders like {currentRepetition} and {totalRepetitions}.
java
@RepeatedTest(3)
void testMultipleTimes() {
    // test code here
}

// With custom name
@RepeatedTest(value = 5, name = "Run {currentRepetition} of {totalRepetitions}")
void testWithCustomName() {
    // test code here
}
๐Ÿ’ป

Example

This example shows a test method that runs 4 times using @RepeatedTest(4). It prints the current repetition number to demonstrate repeated execution.

java
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.TestInfo;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class RepeatedTestExample {

    @RepeatedTest(4)
    void repeatedTestExample(TestInfo testInfo) {
        System.out.println("Running repetition: " + testInfo.getDisplayName());
        assertTrue(true); // simple assertion to pass
    }
}
Output
Running repetition: repeatedTestExample() repeated 1/4 Running repetition: repeatedTestExample() repeated 2/4 Running repetition: repeatedTestExample() repeated 3/4 Running repetition: repeatedTestExample() repeated 4/4
โš ๏ธ

Common Pitfalls

Common mistakes when using @RepeatedTest include:

  • Not importing org.junit.jupiter.api.RepeatedTest.
  • Using @Test and @RepeatedTest together on the same method (only use @RepeatedTest).
  • Forgetting that the test runs multiple times, so side effects or shared state can cause flaky tests.

Always ensure your test is idempotent or resets state before each repetition.

java
/* Wrong: Using both @Test and @RepeatedTest */
//@Test
@RepeatedTest(3)
void wrongTest() {
    // This will cause confusion or errors
}

/* Right: Use only @RepeatedTest */
@RepeatedTest(3)
void correctTest() {
    // Correct usage
}
๐Ÿ“Š

Quick Reference

FeatureDescriptionExample
Basic usageRun test multiple times@RepeatedTest(5)
Custom nameDisplay name with repetition info@RepeatedTest(value=3, name="Run {currentRepetition} of {totalRepetitions}")
Access repetition infoUse TestInfo or RepetitionInfo parametervoid test(RepetitionInfo info) {...}
Avoid @TestDo not combine with @RepeatedTestUse only @RepeatedTest
โœ…

Key Takeaways

Use @RepeatedTest with a number to run a test multiple times automatically.
Do not combine @RepeatedTest with @Test on the same method.
Ensure tests are independent and reset state to avoid flaky results.
Use the optional name parameter to customize repetition display names.
Access repetition details via TestInfo or RepetitionInfo parameters if needed.