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.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
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:
@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
}
}
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.
NOTE: only plugin configuration is taken from provided POM, all other tags are ignored.
- Since:
- 3.4.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope -
Field Summary
FieldsFields inherited from class org.codehaus.plexus.testing.PlexusExtension
BASEDIR_KEY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidafterEach(org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context) static org.codehaus.plexus.util.xml.Xpp3DomextractPluginConfiguration(String artifactId, org.codehaus.plexus.util.xml.Xpp3Dom pomDom) static StringGets the base directory for test resources.protected Stringstatic FilegetTestFile(String path) Gets the file according to base directory for test resources.static StringgetTestPath(String path) Gets the path according to base directory for test resources.getVariablesAndValuesFromObject(Class<?> clazz, 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.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(Object object, 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, String[] coord, org.codehaus.plexus.util.xml.Xpp3Dom pluginConfiguration, org.apache.maven.plugin.descriptor.PluginDescriptor descriptor, Path pomPath) lookup the mojo while we have all the relevent informationprotected String[]mojoCoordinates(String goal, org.apache.maven.plugin.descriptor.PluginDescriptor pluginDescriptor) resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) static voidsetVariableValueToObject(Object object, String variable, 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, setTestBasedirMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Field Details
-
BASEDIR_IS_SET_KEY
- See Also:
-
-
Constructor Details
-
MojoExtension
public MojoExtension()
-
-
Method Details
-
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 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
-
afterEach
-
getPluginDescriptorLocation
-
mojoCoordinates
-
lookupMojo
protected org.apache.maven.plugin.Mojo lookupMojo(org.junit.jupiter.api.extension.ExtensionContext extensionContext, String[] coord, org.codehaus.plexus.util.xml.Xpp3Dom pluginConfiguration, org.apache.maven.plugin.descriptor.PluginDescriptor descriptor, Path pomPath) throws Exception lookup the mojo while we have all the relevent information- Throws:
Exception
-
extractPluginConfiguration
-
getVariableValueFromObject
public static <T> T 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
-
getBasedir
-
getTestFile
-
getTestPath
-
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
-