Dependency injection

In former JUnit versions, test constructors and methods were not allowed to have parameters. One of the major changes in JUnit 5 is that both test constructors and methods are now allowed to include parameters. This feature enables the dependency injection for constructors and methods.

As introduced in Chapter 2, What's New In JUnit 5 of this book, the extension model has an extension that provides dependency injections for Jupiter tests, called ParameterResolver, which defines an API for test extensions that wish to dynamically resolve parameters at runtime.

If a test constructor or a method annotated with @Test, @TestFactory, @BeforeEach, @AfterEach, @BeforeAll, or @AfterAll accepts a parameter, that parameter is resolved at runtime by a resolver (object with parent class ParameterResolver). There are three built-in resolvers registered automatically in JUnit 5: TestInfoParameterResolver, and RepetitionInfoParameterResolver, TestReporterParameterResolver. We review each one of these resolvers in this section.

..................Content has been hidden....................

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