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
    Modifier and Type
    Field
    Description
    protected static String
     
    protected static org.junit.jupiter.api.extension.ExtensionContext
     
    protected Injector
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    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
    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
    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
    lookup(Class<T> componentClass)
    Looks up a component of the specified type from the container.
    protected <T> T
    lookup(Class<T> componentClass, Object qualifier)
    Looks up a component of the specified type and qualifier from the container.
    protected <T> T
    lookup(Class<T> componentClass, String roleHint)
    Looks up a component of the specified type and role hint from the container.
    protected void
    release(Object component)
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • context

      protected static org.junit.jupiter.api.extension.ExtensionContext context
    • injector

      protected Injector injector
    • basedir

      protected static String basedir
  • Constructor Details

    • MavenDIExtension

      public MavenDIExtension()
  • Method Details

    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      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 interface org.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

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Cleans up resources after each test method execution. Currently a placeholder for future cleanup implementation.
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Parameters:
      context - The extension context provided by JUnit
      Throws:
      Exception
    • getInjector

      public Injector getInjector()
      Returns the DI injector, creating it if necessary.
      Returns:
      The configured injector instance
    • lookup

      protected <T> T lookup(Class<T> componentClass) throws DIException
      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

      protected <T> T lookup(Class<T> componentClass, String roleHint) throws 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 up
      roleHint - The role hint for the component
      Returns:
      The component instance
      Throws:
      DIException - if lookup fails
    • lookup

      protected <T> T lookup(Class<T> componentClass, Object qualifier) throws 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 up
      qualifier - The qualifier for the component
      Returns:
      The component instance
      Throws:
      DIException - if lookup fails
    • release

      protected void release(Object component) throws DIException
      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

      public static File getTestFile(String path)
      Creates a File object for a path relative to the base directory.
      Parameters:
      path - The relative path
      Returns:
      A File object representing the path
    • getTestFile

      public static File getTestFile(String basedir, String path)
      Creates a File object for a path relative to a specified base directory.
      Parameters:
      basedir - The base directory path
      path - The relative path
      Returns:
      A File object representing the path
    • getTestPath

      public static String getTestPath(String path)
      Returns the absolute path for a path relative to the base directory.
      Parameters:
      path - The relative path
      Returns:
      The absolute path
    • getTestPath

      public static String getTestPath(String basedir, String path)
      Returns the absolute path for a path relative to a specified base directory.
      Parameters:
      basedir - The base directory path
      path - The relative path
      Returns:
      The absolute path
    • getBasedir

      public static String 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