JUnit 5 @RepeatedTest Annotation Example

JUnit 5 @RepeatedTest annotation enable to write repeatable test templates which could be run multiple times. The frequency can be configured as parameter to @RepeatedTest annotation.

1. @RepeatedTest annotation usage

To create repeatable test template method, annotate the method with @RepeatedTest.

@DisplayName("Add operation test")
@RepeatedTest(5)
void addNumber(TestInfo testInfo) {
	Calculator calculator = new Calculator();
	Assertions.assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
}

In above code, addNumber() test will repeated 5 times.

Please note that each invocation of a repeated test behaves like the execution of a regular @Test method with full support for the same lifecycle callbacks and extensions. It means that @BeforeEach and @AfterEach annotated methods will be invoked where they fit in test life cycle, for each individual invocation.

package com.howtodoinjava.junit5.examples;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
public class RepeatedTestExample {
	
	@BeforeAll
	public static void init(){
		System.out.println("Before All init() method called");
	}
	
	@BeforeEach
	public void initEach(){
		System.out.println("Before Each initEach() method called");
	}

	@DisplayName("Add operation test")
	@RepeatedTest(5)
	void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo) 
	{
		System.out.println("Running addNumber test -> " + repetitionInfo.getCurrentRepetition());
		Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2");
	}
	
	@AfterEach
	public void cleanUpEach(){
		System.out.println("After Each cleanUpEach() method called");
	}
	
	@AfterAll
	public static void cleanUp(){
		System.out.println("After All cleanUp() method called");
	}
}

Output of the above program:

Before All init() method called

Before Each initEach() method called
After Each cleanUpEach() method called

Before Each initEach() method called
Running addNumber test -> 1
After Each cleanUpEach() method called

Before Each initEach() method called
Running addNumber test -> 2
After Each cleanUpEach() method called

Before Each initEach() method called
Running addNumber test -> 3
After Each cleanUpEach() method called

Before Each initEach() method called
Running addNumber test -> 4
After Each cleanUpEach() method called

Before Each initEach() method called
Running addNumber test -> 5
After Each cleanUpEach() method called

After All cleanUp() method called

2. Custom Display Test Names

Apart from specifying the number of repetitions, you can give a custom display name to each repetition. This custom display name can be combination of static text + dynamic place holders. Currently, 3 place holders are supported:

  • {displayName}: display name of the @RepeatedTest method.
  • {currentRepetition}: the current repetition count.
  • {totalRepetitions}: the total number of repetitions.
@RunWith(JUnitPlatform.class)
public class JUnit5AnnotationsExample 
{
	@DisplayName("Add operation test")
	@RepeatedTest(value = 5, name = "{displayName} - repetition {currentRepetition} of {totalRepetitions}")
	void addNumber(TestInfo testInfo) {
		Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2");
	}
}

Running above test will output in below:

JUnit 5 Repeated Test Display Names
JUnit 5 Repeated Test Display Names

You can use one of two predefined formats i.e. RepeatedTest.LONG_DISPLAY_NAME and RepeatedTest.SHORT_DISPLAY_NAME. SHORT_DISPLAY_NAME is default format if none specified.

  • RepeatedTest.LONG_DISPLAY_NAME – {displayName} :: repetition {currentRepetition} of {totalRepetitions}
  • RepeatedTest.SHORT_DISPLAY_NAME – repetition {currentRepetition} of {totalRepetitions}
@DisplayName("Add operation test")
@RepeatedTest(value = 5, name = RepeatedTest.LONG_DISPLAY_NAME)
void addNumber(TestInfo testInfo) {
	Assertions.assertEquals(2, Calculator .add(1, 1), "1 + 1 should equal 2");
}

3. RepetitionInfo interface

RepetitionInfo is used to inject information about the current repetition of a repeated test into @RepeatedTest, @BeforeEach, and @AfterEach methods.

@RunWith(JUnitPlatform.class)
public class JUnit5AnnotationsExample {
	
	@BeforeEach
	public void initEach(RepetitionInfo info){
		int currentRepetition = info.getCurrentRepetition();
        int totalRepetitions = info.getTotalRepetitions();
        //Use information as needed
	}

	@DisplayName("Add operation test")
	@RepeatedTest(value = 5, name="{displayName} :: repetition {currentRepetition} of {totalRepetitions}")
	void addNumber(TestInfo testInfo) {
		Calculator calculator = new Calculator();
		Assertions.assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
	}
	
	@AfterEach
	public void cleanUpEach(RepetitionInfo info){
		int currentRepetition = info.getCurrentRepetition();
        int totalRepetitions = info.getTotalRepetitions();
        //Use information as needed
	}
}

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “JUnit 5 @RepeatedTest Annotation Example”

  1. What is the need to repeat for same input N times. Can you please elaborate if we can use different datasets for every execution?

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.