I have a maven plugin and a simple Mojo that is somewhat close to
public abstract class AbstractSetupMojo extends AbstractMojo { @Parameter(property="targetHost", defaultValue="localhost") private String targetHost; @Parameter(property="targetPort", defaultValue="27017") private Integer targetPort; @Parameter(property="targetDbName", required=true) private String targetDbName; @Parameter(property="sourceHost", defaultValue="${mojo.configuration.targetHost}") private String sourceHost; @Parameter(property="sourcePort", defaultValue="${mojo.configuration.targetPort}") private Integer sourcePort; @Parameter (property="sourceDbName", defaultValue="${mojo.configuration.targetDbName}") private String sourceDbName; @Parameter(property="scriptsPath") private File scriptsPath; }
Other Mojos extend this one. So, the idea is to set the source* parameters to the values ββof the corresponding target* parameters. Some default values ββare also set.
In the test, I have something like
public class GenerateMojoTest extends AbstractMojoTestCase { protected void setUp() throws Exception { super.setUp(); } public void testConfiguration() throws Exception { File pom = getTestFile("src/test/resources/test-project-1/pom.xml"); assertNotNull(pom); assertTrue(pom.exists()); GenerateMojo generateMojo = (GenerateMojo)lookupMojo("generate", pom); assertThat(generateMojo.getSourceDbName()).isEqualTo(generateMojo.getTargetDbName()); assertThat(generateMojo.getSourcePort()).isEqualTo(generateMojo.getTargetPort()).isEqualTo(27017); assertThat(generateMojo.getSourceHost()).isEqualTo(generateMojo.getTargetHost()).isEqualTo("localhost"); } }
Part of the interest in the POM file in the test looks like
<plugin> <groupId>com.ffy</groupId> <artifactId>setup-maven-plugin</artifactId> <executions> <execution> <id>generate</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin>
The test fails because all Mojo parameters are null, if I keep <configuration> commented out, if I uncomment it, then only scriptsPath . Other parameters are null .
Do I need to do something in my tests in order to have a fully configured Mojo?
I tried a longer approach with
protected GenerateMojo setupMojo(final File pom) throws ComponentConfigurationException, Exception { final MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest(); final ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest(); final ProjectBuilder projectBuilder = this.lookup(ProjectBuilder.class); final MavenProject project = projectBuilder.build(pom, buildingRequest).getProject(); final MavenSession session = newMavenSession(project); final MojoExecution execution = newMojoExecution("generate"); final GenerateMojo mojo = (GenerateMojo) this.lookupConfiguredMojo(session, execution); return mojo; }
instead of lookupMojo , but that has changed a bit.