Introduction
In this chapter, we will explore how to execute repeated tests in JUnit. Repeated tests are useful when you want to run the same test multiple times to ensure consistent behavior or to simulate conditions that require multiple executions. JUnit provides the @RepeatedTest annotation to achieve this functionality.
What are Repeated Tests?
Repeated tests allow you to run a single test method multiple times.
This can be useful for:
- Verifying that a method produces consistent results across multiple executions.
- Stress testing a method to ensure it handles repeated calls correctly.
- Detecting flaky tests that may pass or fail intermittently.
Overview of @RepeatedTest Annotation
The @RepeatedTest annotation is used to indicate that a test method should be executed multiple times. You can specify the number of repetitions as an argument to the annotation.
Syntax of @RepeatedTest
@RepeatedTest(value = 10, name = RepeatedTest.LONG_DISPLAY_NAME)
value: The number of times the test method should be repeated.name: The display name for each repetition. You can use placeholders such as{displayName},{currentRepetition}, and{totalRepetitions}to customize the display name.
Example: Creating Repeated Tests
Let’s create examples to demonstrate how to use the @RepeatedTest annotation.
Step 1: Create a Class Under Test
Calculator Class
The Calculator class will have basic arithmetic operations.
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("Division by zero");
}
return a / b;
}
}
Step 2: Create a Test Class with Repeated Tests
CalculatorTest
The CalculatorTest class will contain tests for the Calculator class with repeated test methods.
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import static org.junit.jupiter.api.Assertions.*;
@DisplayName("Calculator Operations Test")
public class CalculatorTest {
private Calculator calculator = new Calculator();
@RepeatedTest(value = 5, name = RepeatedTest.LONG_DISPLAY_NAME)
@DisplayName("Test Addition Repeatedly")
void testAddRepeatedly() {
assertEquals(5, calculator.add(2, 3), "2 + 3 should equal 5");
}
@RepeatedTest(value = 3, name = "{displayName} - repetition {currentRepetition} of {totalRepetitions}")
@DisplayName("Test Subtraction Repeatedly")
void testSubtractRepeatedly() {
assertEquals(2, calculator.subtract(5, 3), "5 - 3 should equal 2");
}
@Test
@DisplayName("Test Multiplication")
void testMultiply() {
assertEquals(6, calculator.multiply(2, 3), "2 * 3 should equal 6");
}
@Test
@DisplayName("Test Division")
void testDivide() {
assertEquals(2, calculator.divide(6, 3), "6 / 3 should equal 2");
}
@Test
@DisplayName("Test Division by Zero Throws Exception")
void testDivideByZero() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
calculator.divide(1, 0);
});
assertEquals("Division by zero", exception.getMessage());
}
}
Important Points
- @RepeatedTest: Use this annotation to specify the number of times a test method should be executed. This helps in verifying the consistent behavior of methods across multiple executions.
- Display Names: Customize the display names for each repetition using the
nameattribute of the@RepeatedTestannotation. Placeholders like{displayName},{currentRepetition}, and{totalRepetitions}can be used to create informative display names.
Step 3: Running the Repeated Tests
To run the repeated tests, simply run the CalculatorTest class as a JUnit test. This will execute all test methods, including those marked with the @RepeatedTest annotation, multiple times.
Using IntelliJ IDEA
- Run Tests: Click the green run icon next to the
CalculatorTestclass and selectRun. - View Results: The results will be displayed in the Run window with the custom display names for repeated tests.

Conclusion
JUnit provides a convenient way to run repeated tests using the @RepeatedTest annotation. Repeated tests are useful for verifying consistent behavior, stress testing, and detecting flaky tests. By using the @RepeatedTest annotation and customizing display names, you can create informative and maintainable test suites for your Java applications.