JUnit Jupiter provides the ability to repeat a test a specified number of times by annotating a method with @RepeatedTest and specifying the total number of repetitions desired.
Every invocation of a repeated test behaves like the execution of a regular @Test method having full support for the same lifecycle callbacks and extensions.
The following example demonstrates how to declare a test named repeatedTest() that will be automatically repeated 5 times.
@RepeatedTest(5)
void repeatedTest() {
// …
}
Let’s try to understand the above concept using a demo project:
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>JUnit5RepeatedTestsExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
RepeatedTestsDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.logging.Logger; 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.TestInfo; public class RepeatedTestsDemo { private static final Logger logger = Logger.getLogger(RepeatedTestsDemo.class.getName()); @BeforeEach void beforeEach(TestInfo testInfo, RepetitionInfo repetitionInfo) { int curRepetition = repetitionInfo.getCurrentRepetition(); int totalRepetitions = repetitionInfo.getTotalRepetitions(); String methodName = testInfo.getTestMethod().get().getName(); logger.info(String.format("Now about to execute repetition %d of %d for %s", curRepetition, totalRepetitions, methodName)); } @RepeatedTest(5) void repeatedTest() { //Testing code Logic goes here } @RepeatedTest(10) void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) { assertEquals(10, repetitionInfo.getTotalRepetitions()); } @RepeatedTest(value = 1, name = "{displayName} {currentRepetition}/{totalRepetitions}") @DisplayName("RepeatTest") void customDisplayName(TestInfo testInfo) { assertEquals("RepeatTest 1/1", testInfo.getDisplayName()); } @RepeatedTest(value = 1, name = RepeatedTest.LONG_DISPLAY_NAME) @DisplayName("RepeatedTestWithLongDisplayName") void customTestNameWithLongDisplayName(TestInfo testInfo) { assertEquals("RepeatedTestWithLongDisplayName :: repetition 1 of 1", testInfo.getDisplayName()); } @RepeatedTest(value = 6, name = "Wiederholung {currentRepetition} von {totalRepetitions}") void repeatedTestInGerman() { //Testing code Logic goes here } } |
The output of the above project:
You May Also Like:
Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
Conditional Test Execution in JUnit 5
JUnit 5 Nested Tests Example
JUnit 5 @Tag Annotation example
Test Execution Order in Junit 5
Dependency Injection and Testing in JUnit 5
Test Interfaces and Default Methods in JUnit 5
That’s all about JUnit 5 @RepeatedTest Annotation example
If you have any feedback or suggestion please feel free to drop in below comment box.