Reputation: 3496
I have a maven plugin and a simple Mojo that looks 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 are extending this one. So, the idea is to set the source*
parameters to the values of the corresponding target*
parameters. Also some default values are 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");
}
}
The part of interest in the POM file in the test looks like
<plugin>
<groupId>com.ffy</groupId>
<artifactId>setup-maven-plugin</artifactId>
<!--
<configuration>
<scriptsPath>src/test/resources/temp</scriptsPath>
</configuration>
-->
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
The test fails because all the Mojo parameters are null if I keep <configuration>
commented out, if I uncomment it then only scriptsPath
is set. Other parameters are null
.
Do I have to do something else in my tests in order to have a fully configured Mojo ?
I tried 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 didn't change a bit.
Upvotes: 6
Views: 2857
Reputation: 32567
You will need to have the <configuration/>
part and define the values you're interested in. You would expect it to be a little smarter, but in fact what the testing harness does is, it reads the values from the <configuration/>
and ignores the ones from your annotations. The testing harness leaves a lot to be desired, it doesn't actually load the values for you like a proper Maven execution/interpolation... Hence, I would advise using the maven-invoker-plugin
, if it better suits your needs.
Upvotes: 5