@RepeatedTest Annotation
Simply add the @RepeatedTest annotation to the top of the test function to create a repeating test:
@RepeatedTest(4)
void executeRepeatTest(TestInfo testInfo) {
System.out.println("Execution of repeated tests");
assertEquals(30, Math.addExact(10, 20), "10 + 20 = 30");
}

You can also try this code with Online Java Compiler
Run Code
For our unit test, we are utilising @RepeatedTest instead of the conventional @Test annotation. The above test will be run three times, as if it were written three times in total.
Lifecycle support for @RepeatTest
The @RepeatedTest will function like a regular @Test, with full JUnit test life cycle support for each run. The @BeforeEach and @AfterEach methods will be invoked before and after each execution. Simply add the following methods to the test class to illustrate this:
@BeforeEach
void printBeforeEachTest() {
System.out.println(" ==== Before each Test printCoding Ninjas ====");
}
@AfterEach
void printAfterEachTest() {
System.out.println("=== After Each Test print Coding Ninjas ===");
System.out.println();
}

You can also try this code with Online Java Compiler
Run Code
As can be seen, the @BeforeEach and @AfterEach methods are called before and after each execution.
Configuring the Test Name
We saw that the test report output did not contain any IDs in the first case.
@RepeatedTest(value = 4, name = RepeatedTest.LONG_DISPLAY_NAME)
void testWithLongName() {
System.out.println("Performing a series of tests with long names");
assertEquals(22, Math.addExact(20, 2), "20 + 2 = 22");
}
@RepeatedTest(value = 4, name = "Modified name {currentRepetition}/{totalRepetitions}")
void testWithCustomDisplayName(TestInfo testInfo) {
assertEquals(50, Math.addExact(20, 30), "20 + 30 = 50");
}

You can also try this code with Online Java Compiler
Run Code
Accessing the RepetitionInfo
JUnit also gives us access to the repetition metadata in our test code, in addition to the name attribute. This is accomplished by including the following RepetitionInfo argument in our test method:
@RepeatedTest(3)
void testWithRepetitionInfo(RepetitionInfo repetitionTestInfo) {
System.out.println("Repetition : " + repetitionTestInfo.getCurrentRepetition());
assertEquals(3, repetitionTestInfo.getTotalRepetitions());
}

You can also try this code with Online Java Compiler
Run Code
RepetitionInfoParameterResolver provides RepetitionInfo, which is only available in the context of @RepeatedTest.
Input
Test Program:
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
public class DemoTest {
@RepeatedTest(4)
void executeRepeatTest(TestInfo testInfo) {
System.out.println("Execution of repeated tests");
assertEquals(30, Math.addExact(10, 20), "10 + 20 = 30");
}
@BeforeEach
void printBeforeEachTest() {
System.out.println(" ==== Before each Test printCoding Ninjas ====");
}
@AfterEach
void printAfterEachTest() {
System.out.println("=== After Each Test print Coding Ninjas ===");
System.out.println();
}
@RepeatedTest(value = 4, name = RepeatedTest.LONG_DISPLAY_NAME)
void testWithLongName() {
System.out.println("Performing a series of tests with long names");
assertEquals(22, Math.addExact(20, 2), "20 + 2 = 22");
}
@RepeatedTest(value = 4, name = "Modified name {currentRepetition}/{totalRepetitions}")
void testWithCustomDisplayName(TestInfo testInfo) {
assertEquals(50, Math.addExact(20, 30), "20 + 30 = 50");
}
@RepeatedTest(3)
void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionTestInfo) {
System.out.println("Repetition : " + repetitionTestInfo.getCurrentRepetition());
assertEquals(3, repetitionTestInfo.getTotalRepetitions());
}
}

You can also try this code with Online Java Compiler
Run Code
Output:

FAQs
-
Does JUnit support repeatable tests?
The @RepeatedTest annotation in JUnit Jupiter is used to repeat the test case for a given number of times. Because each test case iteration behaves like a standard @Test method, it supports the same JUnit 5 lifecycle callbacks and extensions.
-
How do you repeat a test in JUnit?
The easiest approach to use the annotation @RepeatedTest is to supply an integer number as an input parameter, such as @RepeatedTest(5). The test function multiply() is run 5 times during execution, according to the Report tab of the JUnit output.
-
What does @repeated test do?
With a customizable display name, the @RepeatedTest annotation is used to designate a test method that should repeat a defined number of times. Consider using @ParameterizedTest to rerun a test with various parameters.
-
In JUnit 5, how do you write a test case?
Writing Test Suites is a difficult task. You may run tests that span many test classes and packages using JUnit 5 test suites. These annotations are available in JUnit 5 for creating test suites. To run the suite, add the JUnit-platform-suite module to the project dependencies and use the @Suite annotation.
Key Takeaways
If you have reached till here, you really enjoyed reading this article. This post went through every aspect of repeating a test many times. Also, during repeat test automation, I attempted to react to certain critical annotation-related queries. The configuration for the @RepeatedTest annotation is covered in this article.
Hope you like reading this article, you must also be interested in the article such as migration from junit4 to junit5 and the difference between JUnit and TestNG between never give up on your quest for knowledge.
Happy Learning!