RepetitionInfoParameterResolver

The second resolver provided out of the box in JUnit 5 is called RepetitionInfoParameterResolver. Given a test class, if a method parameter in a @RepeatedTest, @BeforeEach, or @AfterEach method is of type RepetitionInfo, the RepetitionInfoParameterResolver will supply an instance of RepetitionInfo.

RepetitionInfo can be used to retrieve information about the current repetition and the total number of repetitions for the corresponding @RepeatedTest. The API of RepetitionInfo offers two methods, as shown in the screenshot after the list:

  • int getCurrentRepetition(): Gets the current repetition of the corresponding @RepeatedTest method
  • int getTotalRepetitions(): Gets the total number of repetitions of the corresponding @RepeatedTest method
RepetitionInfo API

The class here contains a simple example for the use of RepetitionInfo:

package io.github.bonigarcia;

import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;

class RepetitionInfoTest {

@RepeatedTest(2)
void test(RepetitionInfo repetitionInfo) {
System.out.println("** Test " +
repetitionInfo.getCurrentRepetition()
+ "/" + repetitionInfo.getTotalRepetitions());
}

}

As can be seen in the test output, we are able to read the information about the repeated test at runtime:

The console output of dependency injection of RepetitionInfo objects.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.35.247