TestInfo parameter

This lesson demonstrates how to use dependency injection (TestInfo Parameter) in Constructor and Test methods.


In previous JUnit versions, there was limited/no support to allow parameters in test constructors or methods. One of the major changes in JUnit 5 Jupiter was that both test constructors and test methods are now allowed to have parameters. These parameters provide metadata to constructors and test methods. Thus, allows for greater flexibility and enables Dependency Injection for test methods and constructors.

In Junit 5, there is an interface in org.junit.jupiter.api.extension package by name, ParameterResolver. This interface defines the API for test extensions that wish to dynamically resolve parameters at runtime.

There are different types of ParameterResolver’s in Junit 5. Generally, if a test constructor or a @Test, @BeforeEach, @AfterEach, @BeforeAll, @AfterAll or @TestFactory method accepts a parameter, the parameter must be resolved at runtime by a registered ParameterResolver.

Read more - https://junit.org/junit5/docs/5.3.2/api/org/junit/jupiter/api/extension/package-summary.html for org.junit.jupiter.api.extension

https://junit.org/junit5/docs/5.3.2/api/org/junit/jupiter/api/extension/ParameterResolver.html for ParameterResolver.

Get hands-on with 1200+ tech skills courses.