Parameter Resolver: TestInfo

Understand how to use dependency injection (TestInfo parameter) in Constructor and Test methods.

What’s dependency injection?

It’s common for a test method to use external dependencies. In JUnit 5, test constructors and methods can have parameters to inject dependencies. However, not all types of parameters are allowed in constructors or methods. Extensions should be defined to resolve the parameters. The extension model allows for registering custom parameter resolvers. In this category, we only cover the built-in parameter resolvers. The org.junit.jupiter.api.extension.ParameterResolver is the interface for parameter resolvers.

JUnit 5 has three built-in parameter resolvers:

  • TestInfoParameterResolver
  • TestReporterParameterResolver
  • RepetitionInfoParameterResolver

Get hands-on with 1400+ tech skills courses.