JUnit Repeated Tests - @RepeatedTest With Examples

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.

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.

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:

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.

It will print following console messages:

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

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.

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.

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


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.

By admin

Leave a Reply

%d bloggers like this: