JUnit Repeated Tests – @RepeatedTest

Filed Under: JUnit

JUnit Jupiter provides @RepeatedTest annotation that lets us repeat a test specified number of times.

JUnit Repeated Tests Example

Let’s have a look at a very simple example of JUnit 5 Repeated Tests.


@RepeatedTest(5)
void test() {
	System.out.println("@RepeatedTest Simple Example");
}

When you will run above test method, it will be called 5 times. You can check the console message or JUnit view in Eclipse to confirm this.

JUnit Repeated Tests Example

Did you noticed that the tests display name is of the format repitition X of N. It’s because RepeatedTest changes the Display Name of the method internally. The default value of RepeatedTest name is picked from below variable in the annotation:


String SHORT_DISPLAY_NAME = "repetition " + {currentRepetition} + " of " + {totalRepetitions};

JUnit @RepeatedTest Display Name

We can change the @RepeatedTest Display Name value to any static name. It also supports three dynamic placeholders.

  1. {displayName}: Picked from @DisplayName value.
  2. {currentRepetition}: the current repetition count
  3. {totalRepetitions}: the total number of repetitions

Here is an example of RepeatedTest name using dynamic placeholders.


@RepeatedTest(value=2, name="{displayName} {currentRepetition}/{totalRepetitions}")
@DisplayName("MyTest")
void test_with_cutom_DisplayName(TestInfo testInfo) {
	System.out.println(testInfo.getDisplayName());
}

It will print following console messages:


MyTest 1/2
MyTest 2/2

Below image shows the JUnit view where method names are displayed.

JUnit Repeated Test Dynamic Placeholder name

JUnit @RepetitionInfo

Did you noticed that I injected TestInfo into my test method. JUnit Jupiter provides @RepetitionInfo annotation that we can inject into our test method.


@RepeatedTest(3)
void test_with_RepetitionInfo_Injection(RepetitionInfo repetitionInfo) {
	System.out.println("@RepeatedTest with RepetitionInfo Injection");
	assertEquals(3, repetitionInfo.getTotalRepetitions());
	
	System.out.println("Current Test Count = "+repetitionInfo.getCurrentRepetition());
}

We are printing current repetition count using RepetitionInfo. If we have many such methods, then we can move it to @BeforeEach or @AfterEach methods too.


@BeforeEach
void setUp(RepetitionInfo repetitionInfo, TestInfo testInfo) {
	System.out.println("Method = "+testInfo.getTestMethod().get().getName()+", Execution Count = "+repetitionInfo.getCurrentRepetition());
}

Above @BeforeEach method will throw an error if all the test methods are not annotated with @RepeatedTest.


org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter 
[org.junit.jupiter.api.RepetitionInfo arg0] in executable 
[void com.journaldev.repeatedtests.RepeatedTestExample.setUp
(org.junit.jupiter.api.RepetitionInfo,org.junit.jupiter.api.TestInfo)]

Summary

JUnit @RepeatedTest is a useful annotation and we can use it to test our methods multiple times with same data. However, most of the times we want to run our tests multiple times with different data. You should look into JUnit @ParameterizedTest to achieve this.

You can download the complete code from our GitHub Repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages