Class 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 org.codehaus.plexus.testing.PlexusExtension
    implements org.junit.jupiter.api.extension.ParameterResolver
    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:

     {@code
    Since:
    3.4.0
    See Also:
    MojoTest, InjectMojo, MojoParameter, Basedir
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension

        org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String BASEDIR_IS_SET_KEY  
      • Fields inherited from class org.codehaus.plexus.testing.PlexusExtension

        BASEDIR_KEY
    • Constructor Summary

      Constructors 
      Constructor Description
      MojoExtension()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void afterEach​(org.junit.jupiter.api.extension.ExtensionContext context)  
      void beforeEach​(org.junit.jupiter.api.extension.ExtensionContext context)  
      static org.codehaus.plexus.util.xml.Xpp3Dom extractPluginConfiguration​(java.lang.String artifactId, org.codehaus.plexus.util.xml.Xpp3Dom pomDom)  
      static java.lang.String getBasedir()
      Gets the base directory for test resources.
      protected java.lang.String getPluginDescriptorLocation()  
      static java.io.File getTestFile​(java.lang.String path)
      Gets the file according to base directory for test resources.
      static java.lang.String getTestPath​(java.lang.String path)
      Gets the path according to base directory for test resources.
      static java.util.Map<java.lang.String,​java.lang.Object> getVariablesAndValuesFromObject​(java.lang.Class<?> clazz, java.lang.Object object)
      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.
      static java.util.Map<java.lang.String,​java.lang.Object> getVariablesAndValuesFromObject​(java.lang.Object object)
      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.
      static <T> T getVariableValueFromObject​(java.lang.Object object, java.lang.String variable)
      Convenience method to obtain the value of a variable on a mojo that might not have a getter.
      protected org.apache.maven.plugin.Mojo lookupMojo​(org.junit.jupiter.api.extension.ExtensionContext extensionContext, java.lang.String[] coord, org.codehaus.plexus.util.xml.Xpp3Dom pluginConfiguration, org.apache.maven.plugin.descriptor.PluginDescriptor descriptor, java.nio.file.Path pomPath)
      lookup the mojo while we have all the relevent information
      protected java.lang.String[] mojoCoordinates​(java.lang.String goal, org.apache.maven.plugin.descriptor.PluginDescriptor pluginDescriptor)  
      java.lang.Object resolveParameter​(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)  
      static void setVariableValueToObject​(java.lang.Object object, java.lang.String variable, java.lang.Object value)
      Convenience method to set values to variables in objects that don't have setters
      boolean supportsParameter​(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)  
      • Methods inherited from class org.codehaus.plexus.testing.PlexusExtension

        customizeContainerConfiguration, customizeContext, getConfigurationName, getContainer, getCustomConfigurationName, getTestBasedir, getTestConfiguration, getTestFile, getTestPath, setTestBasedir
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension

        getTestInstantiationExtensionContextScope
    • Field Detail

      • BASEDIR_IS_SET_KEY

        public static final java.lang.String BASEDIR_IS_SET_KEY
        See Also:
        Constant Field Values
    • Constructor Detail

      • MojoExtension

        public MojoExtension()
    • Method Detail

      • 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
        Specified by:
        supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
        Throws:
        org.junit.jupiter.api.extension.ParameterResolutionException
      • resolveParameter

        public java.lang.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 java.lang.Exception
        Specified by:
        beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
        Overrides:
        beforeEach in class org.codehaus.plexus.testing.PlexusExtension
        Throws:
        java.lang.Exception
      • afterEach

        public void afterEach​(org.junit.jupiter.api.extension.ExtensionContext context)
                       throws java.lang.Exception
        Specified by:
        afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
        Overrides:
        afterEach in class org.codehaus.plexus.testing.PlexusExtension
        Throws:
        java.lang.Exception
      • getPluginDescriptorLocation

        protected java.lang.String getPluginDescriptorLocation()
      • mojoCoordinates

        protected java.lang.String[] mojoCoordinates​(java.lang.String goal,
                                                     org.apache.maven.plugin.descriptor.PluginDescriptor pluginDescriptor)
                                              throws java.lang.Exception
        Throws:
        java.lang.Exception
      • lookupMojo

        protected org.apache.maven.plugin.Mojo lookupMojo​(org.junit.jupiter.api.extension.ExtensionContext extensionContext,
                                                          java.lang.String[] coord,
                                                          org.codehaus.plexus.util.xml.Xpp3Dom pluginConfiguration,
                                                          org.apache.maven.plugin.descriptor.PluginDescriptor descriptor,
                                                          java.nio.file.Path pomPath)
                                                   throws java.lang.Exception
        lookup the mojo while we have all the relevent information
        Throws:
        java.lang.Exception
      • extractPluginConfiguration

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

        public static <T> T getVariableValueFromObject​(java.lang.Object object,
                                                       java.lang.String variable)
                                                throws java.lang.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:
        java.lang.IllegalAccessException
      • getVariablesAndValuesFromObject

        public static java.util.Map<java.lang.String,​java.lang.Object> getVariablesAndValuesFromObject​(java.lang.Object object)
                                                                                                      throws java.lang.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:
        java.lang.IllegalAccessException
      • getVariablesAndValuesFromObject

        public static java.util.Map<java.lang.String,​java.lang.Object> getVariablesAndValuesFromObject​(java.lang.Class<?> clazz,
                                                                                                             java.lang.Object object)
                                                                                                      throws java.lang.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:
        java.lang.IllegalAccessException
      • getBasedir

        public static java.lang.String getBasedir()
        Gets the base directory for test resources. If not explicitly set via Basedir, returns the plugin base directory.
      • getTestFile

        public static java.io.File getTestFile​(java.lang.String path)
        Gets the file according to base directory for test resources.
      • getTestPath

        public static java.lang.String getTestPath​(java.lang.String path)
        Gets the path according to base directory for test resources.
      • setVariableValueToObject

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