Class MojoExtension
- java.lang.Object
-
- org.codehaus.plexus.testing.PlexusExtension
-
- 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 org.codehaus.plexus.testing.PlexusExtension implements org.junit.jupiter.api.extension.ParameterResolverJUnit 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
MojoTestannotation on a test class. It provides the following features:- Automatic Mojo instantiation based on
InjectMojoannotations - Parameter injection using
MojoParameterannotations - 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
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringBASEDIR_IS_SET_KEY
-
Constructor Summary
Constructors Constructor Description MojoExtension()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterEach(org.junit.jupiter.api.extension.ExtensionContext context)voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context)static org.codehaus.plexus.util.xml.Xpp3DomextractPluginConfiguration(java.lang.String artifactId, org.codehaus.plexus.util.xml.Xpp3Dom pomDom)static java.lang.StringgetBasedir()Gets the base directory for test resources.protected java.lang.StringgetPluginDescriptorLocation()static java.io.FilegetTestFile(java.lang.String path)Gets the file according to base directory for test resources.static java.lang.StringgetTestPath(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> TgetVariableValueFromObject(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.MojolookupMojo(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 informationprotected java.lang.String[]mojoCoordinates(java.lang.String goal, org.apache.maven.plugin.descriptor.PluginDescriptor pluginDescriptor)java.lang.ObjectresolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)static voidsetVariableValueToObject(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 settersbooleansupportsParameter(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
-
-
-
-
Field Detail
-
BASEDIR_IS_SET_KEY
public static final java.lang.String BASEDIR_IS_SET_KEY
- See Also:
- Constant Field Values
-
-
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:
supportsParameterin interfaceorg.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:
resolveParameterin interfaceorg.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:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback- Overrides:
beforeEachin classorg.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:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback- Overrides:
afterEachin classorg.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.Exceptionlookup 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.IllegalAccessExceptionConvenience 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.IllegalAccessExceptionConvenience 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.IllegalAccessExceptionConvenience 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 viaBasedir, 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.IllegalAccessExceptionConvenience method to set values to variables in objects that don't have setters- Throws:
java.lang.IllegalAccessException
-
-