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 TypeMethodDescriptionvoidafterEach(org.junit.jupiter.api.extension.ExtensionContext context) Cleans up resources after each test method execution.voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context) Initializes the test environment before each test method execution.static StringReturns the base directory for test execution.org.apache.maven.di.InjectorReturns the DI injector, creating it if necessary.static FilegetTestFile(String path) Creates a File object for a path relative to the base directory.static FilegetTestFile(String basedir, String path) Creates a File object for a path relative to a specified base directory.static StringgetTestPath(String path) Returns the absolute path for a path relative to the base directory.static StringgetTestPath(String basedir, String path) Returns the absolute path for a path relative to a specified base directory.protected <T> TLooks up a component of the specified type from the container.protected <T> TLooks up a component of the specified type and qualifier from the container.protected <T> TLooks up a component of the specified type and role hint from the container.protected voidReleases a component back to the container.protected voidsetContext(org.junit.jupiter.api.extension.ExtensionContext context) Stores the extension context for use during test execution.protected voidCreates and configures the DI container for test execution.
-
Field Details
-
context
protected static org.junit.jupiter.api.extension.ExtensionContext context -
injector
protected org.apache.maven.di.Injector 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:
beforeEachin 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:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback- Parameters:
context- The extension context provided by JUnit- Throws:
Exception
-
getInjector
public org.apache.maven.di.Injector 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:
org.apache.maven.di.impl.DIException- if lookup fails
-
lookup
protected <T> T lookup(Class<T> componentClass, String roleHint) throws org.apache.maven.di.impl.DIException 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:
org.apache.maven.di.impl.DIException- if lookup fails
-
lookup
protected <T> T lookup(Class<T> componentClass, Object qualifier) throws org.apache.maven.di.impl.DIException 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:
org.apache.maven.di.impl.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:
org.apache.maven.di.impl.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
-