Class MojoExtension

java.lang.Object
org.apache.maven.api.di.testing.MavenDIExtension
org.apache.maven.api.plugin.testing.MojoExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.TestInstantiationAwareExtension

public class MojoExtension extends MavenDIExtension implements org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.BeforeEachCallback
JUnit Jupiter extension that provides support for testing Maven plugins (Mojos). This extension handles the lifecycle of Mojo instances in tests, including instantiation, configuration, and dependency injection.

The extension is automatically registered when using the MojoTest annotation on a test class. It provides the following features:

  • Automatic Mojo instantiation based on InjectMojo annotations
  • Parameter injection using MojoParameter annotations
  • POM configuration handling
  • Project stub creation and configuration
  • Maven session and build context setup
  • Component dependency injection

Example usage in a test class:

 
 @MojoTest
 class MyMojoTest {
     @Test
     @InjectMojo(goal = "my-goal")
     @MojoParameter(name = "outputDirectory", value = "${project.build.directory}/generated")
     void testMojoExecution(MyMojo mojo) throws Exception {
         mojo.execute();
         // verify execution results
     }
 }
 
 

The extension supports two main injection scenarios:

  1. Method parameter injection: Mojo instances can be injected as test method parameters
  2. Field injection: Components can be injected into test class fields using @Inject

For custom POM configurations, you can specify a POM file using the InjectMojo.pom() attribute. The extension will merge this configuration with default test project settings.

Base directory handling:

  • Plugin basedir: The directory containing the plugin project
  • Test basedir: The directory containing test resources, configurable via Basedir
Since:
4.0.0
See Also:
  • Field Details

    • pluginBasedir

      protected static String pluginBasedir
      The base directory of the plugin being tested
    • basedir

      protected static String basedir
      The base directory for test resources
  • Constructor Details

    • MojoExtension

      public MojoExtension()
  • Method Details

    • getTestId

      public static String getTestId()
      Gets the identifier for the current test method. The format is "TestClassName-testMethodName".
      Returns:
      the test identifier
    • getBasedir

      public static String getBasedir()
      Gets the base directory for test resources. If not explicitly set via Basedir, returns the plugin base directory.
      Returns:
      the base directory path
      Throws:
      NullPointerException - if neither basedir nor plugin basedir is set
    • getPluginBasedir

      public static String getPluginBasedir()
      Gets the base directory of the plugin being tested.
      Returns:
      the plugin base directory path
      Throws:
      NullPointerException - if plugin basedir is not set
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Determines if this extension can resolve the given parameter. Returns true if the parameter is annotated with InjectMojo or if its declaring method is annotated with InjectMojo.
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Parameters:
      parameterContext - the context for the parameter being resolved
      extensionContext - the current extension context
      Returns:
      true if this extension can resolve the parameter
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Description copied from class: MavenDIExtension
      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
      Overrides:
      beforeEach in class MavenDIExtension
      Parameters:
      context - The extension context provided by JUnit
      Throws:
      Exception - if initialization fails
    • getPluginDescriptorLocation

      protected String getPluginDescriptorLocation()
    • mojoCoordinates

      protected String[] mojoCoordinates(String goal) throws Exception
      Throws:
      Exception
    • extractPluginConfiguration

      public static XmlNode extractPluginConfiguration(String artifactId, org.codehaus.plexus.util.xml.Xpp3Dom pomDom) throws Exception
      Throws:
      Exception
    • getVariableValueFromObject

      public static Object getVariableValueFromObject(Object object, String variable) throws IllegalAccessException
      Convenience method to obtain the value of a variable on a mojo that might not have a getter.
      NOTE: the caller is responsible for casting to what the desired type is.
      Throws:
      IllegalAccessException
    • getVariablesAndValuesFromObject

      public static Map<String,Object> getVariablesAndValuesFromObject(Object object) throws IllegalAccessException
      Convenience method to obtain all variables and values from the mojo (including its superclasses)
      Note: the values in the map are of type Object so the caller is responsible for casting to desired types.
      Throws:
      IllegalAccessException
    • getVariablesAndValuesFromObject

      public static Map<String,Object> getVariablesAndValuesFromObject(Class<?> clazz, Object object) throws IllegalAccessException
      Convenience method to obtain all variables and values from the mojo (including its superclasses)
      Note: the values in the map are of type Object so the caller is responsible for casting to desired types.
      Returns:
      map of variable names and values
      Throws:
      IllegalAccessException
    • setVariableValueToObject

      public static void setVariableValueToObject(Object object, String variable, Object value) throws IllegalAccessException
      Convenience method to set values to variables in objects that don't have setters
      Throws:
      IllegalAccessException