Class MavenDIExtension
java.lang.Object
org.apache.maven.api.di.testing.MavenDIExtension
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback
,org.junit.jupiter.api.extension.BeforeEachCallback
,org.junit.jupiter.api.extension.Extension
- Direct Known Subclasses:
MojoExtension
public class MavenDIExtension
extends Object
implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback
JUnit Jupiter extension that provides dependency injection support for Maven tests.
This extension manages the lifecycle of a DI container for each test method execution,
automatically performing injection into test instances and cleanup.
This is a modernized version of the original Plexus test support, adapted for Maven's new DI framework and JUnit Jupiter.
Usage example:
@ExtendWith(MavenDIExtension.class)
class MyTest {
@Inject
private MyComponent component;
@Test
void testSomething() {
// component is automatically injected
}
}
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
afterEach
(org.junit.jupiter.api.extension.ExtensionContext context) Cleans up resources after each test method execution.void
beforeEach
(org.junit.jupiter.api.extension.ExtensionContext context) Initializes the test environment before each test method execution.static String
Returns the base directory for test execution.Returns the DI injector, creating it if necessary.static File
getTestFile
(String path) Creates a File object for a path relative to the base directory.static File
getTestFile
(String basedir, String path) Creates a File object for a path relative to a specified base directory.static String
getTestPath
(String path) Returns the absolute path for a path relative to the base directory.static String
getTestPath
(String basedir, String path) Returns the absolute path for a path relative to a specified base directory.protected <T> T
Looks up a component of the specified type from the container.protected <T> T
Looks up a component of the specified type and qualifier from the container.protected <T> T
Looks up a component of the specified type and role hint from the container.protected void
Releases a component back to the container.protected void
setContext
(org.junit.jupiter.api.extension.ExtensionContext context) Stores the extension context for use during test execution.protected void
Creates and configures the DI container for test execution.
-
Field Details
-
context
protected static org.junit.jupiter.api.extension.ExtensionContext context -
injector
-
basedir
-
-
Constructor Details
-
MavenDIExtension
public MavenDIExtension()
-
-
Method Details
-
beforeEach
Initializes the test environment before each test method execution. Sets up the base directory and DI container, then performs injection into the test instance.- Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
- Parameters:
context
- The extension context provided by JUnit- Throws:
Exception
- if initialization fails
-
setContext
protected void setContext(org.junit.jupiter.api.extension.ExtensionContext context) Stores the extension context for use during test execution.- Parameters:
context
- The extension context to store
-
setupContainer
protected void setupContainer()Creates and configures the DI container for test execution. Performs component discovery and sets up basic bindings.- Throws:
IllegalArgumentException
- if container setup fails
-
afterEach
Cleans up resources after each test method execution. Currently a placeholder for future cleanup implementation.- Specified by:
afterEach
in interfaceorg.junit.jupiter.api.extension.AfterEachCallback
- Parameters:
context
- The extension context provided by JUnit- Throws:
Exception
-
getInjector
Returns the DI injector, creating it if necessary.- Returns:
- The configured injector instance
-
lookup
Looks up a component of the specified type from the container.- Type Parameters:
T
- The component type- Parameters:
componentClass
- The class of the component to look up- Returns:
- The component instance
- Throws:
DIException
- if lookup fails
-
lookup
Looks up a component of the specified type and role hint from the container.- Type Parameters:
T
- The component type- Parameters:
componentClass
- The class of the component to look uproleHint
- The role hint for the component- Returns:
- The component instance
- Throws:
DIException
- if lookup fails
-
lookup
Looks up a component of the specified type and qualifier from the container.- Type Parameters:
T
- The component type- Parameters:
componentClass
- The class of the component to look upqualifier
- The qualifier for the component- Returns:
- The component instance
- Throws:
DIException
- if lookup fails
-
release
Releases a component back to the container. Currently a placeholder for future implementation.- Parameters:
component
- The component to release- Throws:
DIException
- if release fails
-
getTestFile
-
getTestFile
-
getTestPath
-
getTestPath
-
getBasedir
Returns the base directory for test execution. Uses the "basedir" system property if set, otherwise uses the current directory.- Returns:
- The base directory path
-