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 SummaryFields
- 
Constructor SummaryConstructors
- 
Method SummaryModifier 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.Returns 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- 
contextprotected static org.junit.jupiter.api.extension.ExtensionContext context
- 
injector
- 
basedir
 
- 
- 
Constructor Details- 
MavenDIExtensionpublic MavenDIExtension()
 
- 
- 
Method Details- 
beforeEachInitializes 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 interface- org.junit.jupiter.api.extension.BeforeEachCallback
- Parameters:
- context- The extension context provided by JUnit
- Throws:
- Exception- if initialization fails
 
- 
setContextprotected 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
 
- 
setupContainerprotected 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
 
- 
afterEachCleans up resources after each test method execution. Currently a placeholder for future cleanup implementation.- Specified by:
- afterEachin interface- org.junit.jupiter.api.extension.AfterEachCallback
- Parameters:
- context- The extension context provided by JUnit
- Throws:
- Exception
 
- 
getInjectorReturns the DI injector, creating it if necessary.- Returns:
- The configured injector instance
 
- 
lookupLooks 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
 
- 
lookupLooks 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 up
- roleHint- The role hint for the component
- Returns:
- The component instance
- Throws:
- DIException- if lookup fails
 
- 
lookupLooks 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 up
- qualifier- The qualifier for the component
- Returns:
- The component instance
- Throws:
- DIException- if lookup fails
 
- 
releaseReleases 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
- 
getBasedirReturns the base directory for test execution. Uses the "basedir" system property if set, otherwise uses the current directory.- Returns:
- The base directory path
 
 
-