Table of contents
1.
Introduction
2.
Dependency Setup
2.1.
The build.gradle file:
3.
@RepeatedTest Annotation
4.
Lifecycle support for @RepeatTest
5.
Configuring the Test Name
6.
Accessing the RepetitionInfo
6.1.
Input
6.1.1.
Test Program:
6.1.2.
Output:
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

JUnit5 Repeated Tests

Author Aman Thakur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The term 'repeated test' refers to the process of doing specified tests or tests for a set number of times. The same goal may be accomplished with parameterization. The primary difference between a parameterized test and a repeated test is that parameterized tests work with different data sets, whereas repeated tests operate with the same data set. In comparison to parameterization, the repeated test is a feature that is rarely used.

If you need an example of a good use case for repeated tests, consider a situation in which you're automating an application and you navigate to a certain page or an explorer link, which causes an environmental issue that causes your tests to fail.

We'll look into the @RepeatedTest annotation introduced in JUnit 5 in this quick article. It gives us a great tool for writing any test that we wish to run several times.

Dependency Setup

The build.gradle file:

The highlight text is the modifications to be included in the build.gradle file.

plugins {
   id 'java'
}


group 'org.example'
version '1.0-SNAPSHOT'


sourceCompatibility = 1.8


repositories {
   mavenCentral()
}


dependencies {
   testImplementation group: 'org.JUnit.jupiter', name: 'JUnit-jupiter', version: '5.8.2'
}


test {
   useJUnitPlatform()
   testLogging {
       events "passed", "skipped", "failed"
   }
}

@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

  1. 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.
     
  2. 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.
     
  3. 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.
     
  4. 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!

Live masterclass