2.12. Dependency Injection for Constructors and Methods

In all prior JUnit versions, test constructors or methods were not allowed to have parameters (at least not with the standard Runner implementations). As one of the major changes in JUnit Jupiter, both test constructors and methods are now permitted to have parameters. This allows for greater flexibility and enables Dependency Injection for constructors and methods.

[ParameterResolver](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ParameterResolver.html) defines the API for test extensions that wish to dynamically resolve parameters at runtime. If a test class constructor, a test method, or a lifecycle method (see Test Classes and Methods) accepts a parameter, the parameter must be resolved at runtime by a registered ParameterResolver.

There are currently three built-in resolvers that are registered automatically.

  • [TestInfoParameterResolver](https://github.com/junit-team/junit5/tree/r5.7.0/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TestInfoParameterResolver.java): if a constructor or method parameter is of type [TestInfo](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/TestInfo.html), the TestInfoParameterResolver will supply an instance of TestInfo corresponding to the current container or test as the value for the parameter. The TestInfo can then be used to retrieve information about the current container or test such as the display name, the test class, the test method, and associated tags. The display name is either a technical name, such as the name of the test class or test method, or a custom name configured via @DisplayName.

    [TestInfo](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/TestInfo.html) acts as a drop-in replacement for the TestName rule from JUnit 4. The following demonstrates how to have TestInfo injected into a test constructor, @BeforeEach method, and @Test method.

  1. import static org.junit.jupiter.api.Assertions.assertEquals;
  2. import static org.junit.jupiter.api.Assertions.assertTrue;
  3. import org.junit.jupiter.api.BeforeEach;
  4. import org.junit.jupiter.api.DisplayName;
  5. import org.junit.jupiter.api.Tag;
  6. import org.junit.jupiter.api.Test;
  7. import org.junit.jupiter.api.TestInfo;
  8. @DisplayName("TestInfo Demo")
  9. class TestInfoDemo {
  10. TestInfoDemo(TestInfo testInfo) {
  11. assertEquals("TestInfo Demo", testInfo.getDisplayName());
  12. }
  13. @BeforeEach
  14. void init(TestInfo testInfo) {
  15. String displayName = testInfo.getDisplayName();
  16. assertTrue(displayName.equals("TEST 1") || displayName.equals("test2()"));
  17. }
  18. @Test
  19. @DisplayName("TEST 1")
  20. @Tag("my-tag")
  21. void test1(TestInfo testInfo) {
  22. assertEquals("TEST 1", testInfo.getDisplayName());
  23. assertTrue(testInfo.getTags().contains("my-tag"));
  24. }
  25. @Test
  26. void test2() {
  27. }
  28. }
  • [RepetitionInfoParameterResolver](https://github.com/junit-team/junit5/tree/r5.7.0/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/RepetitionInfoParameterResolver.java): if a method parameter in a @RepeatedTest, @BeforeEach, or @AfterEach method is of type [RepetitionInfo](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/RepetitionInfo.html), the RepetitionInfoParameterResolver will supply an instance of RepetitionInfo. RepetitionInfo can then be used to retrieve information about the current repetition and the total number of repetitions for the corresponding @RepeatedTest. Note, however, that RepetitionInfoParameterResolver is not registered outside the context of a @RepeatedTest. See Repeated Test Examples.

  • [TestReporterParameterResolver](https://github.com/junit-team/junit5/tree/r5.7.0/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TestReporterParameterResolver.java): if a constructor or method parameter is of type [TestReporter](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/TestReporter.html), the TestReporterParameterResolver will supply an instance of TestReporter. The TestReporter can be used to publish additional data about the current test run. The data can be consumed via the reportingEntryPublished() method in a [TestExecutionListener](https://junit.org/junit5/docs/current/api/org.junit.platform.launcher/org/junit/platform/launcher/TestExecutionListener.html), allowing it to be viewed in IDEs or included in reports.

    In JUnit Jupiter you should use TestReporter where you used to print information to stdout or stderr in JUnit 4. Using @RunWith(JUnitPlatform.class) will output all reported entries to stdout. In addition, some IDEs print report entries to stdout or display them in the user interface for test results.

  1. class TestReporterDemo {
  2. @Test
  3. void reportSingleValue(TestReporter testReporter) {
  4. testReporter.publishEntry("a status message");
  5. }
  6. @Test
  7. void reportKeyValuePair(TestReporter testReporter) {
  8. testReporter.publishEntry("a key", "a value");
  9. }
  10. @Test
  11. void reportMultipleKeyValuePairs(TestReporter testReporter) {
  12. Map<String, String> values = new HashMap<>();
  13. values.put("user name", "dk38");
  14. values.put("award year", "1974");
  15. testReporter.publishEntry(values);
  16. }
  17. }
Other parameter resolvers must be explicitly enabled by registering appropriate extensions via @ExtendWith.

Check out the [RandomParametersExtension](https://github.com/junit-team/junit5-samples/tree/r5.7.0/junit5-jupiter-extensions/src/main/java/com/example/random/RandomParametersExtension.java) for an example of a custom [ParameterResolver](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ParameterResolver.html). While not intended to be production-ready, it demonstrates the simplicity and expressiveness of both the extension model and the parameter resolution process. MyRandomParametersTest demonstrates how to inject random values into @Test methods.

  1. @ExtendWith(RandomParametersExtension.class)
  2. class MyRandomParametersTest {
  3. @Test
  4. void injectsInteger(@Random int i, @Random int j) {
  5. assertNotEquals(i, j);
  6. }
  7. @Test
  8. void injectsDouble(@Random double d) {
  9. assertEquals(0.0, d, 1.0);
  10. }
  11. }

For real-world use cases, check out the source code for the [MockitoExtension](https://github.com/mockito/mockito/blob/release/2.x/subprojects/junit-jupiter/src/main/java/org/mockito/junit/jupiter/MockitoExtension.java) and the [SpringExtension](https://github.com/spring-projects/spring-framework/tree/HEAD/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java).

When the type of the parameter to inject is the only condition for your [ParameterResolver](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ParameterResolver.html), you can use the generic [TypeBasedParameterResolver](https://github.com/junit-team/junit5/tree/r5.7.0/junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/support/TypeBasedParameterResolver.java) base class. The supportsParameters method is implemented behind the scenes and supports parameterized types.