问题
Based on the documentation and mailing threads I have seen 3 ways to inject the maven project into my mojo:
/**
* Project instance
*
* @parameter default-value="${project}"
* @required
* @readonly
*/
private MavenProject project;
@Component
private MavenProject project;
@Parameter( expression = "${project}" )
private MavenProject project;
But regardless of which one of these I choose, when I run the unit test based on the example I found in maven docs, project is always null.
public void testMojoGoal() throws Exception {
File testPom = new File(getBasedir(),
"src/test/resources/unit/basic-test/sample-sh-project-config.xml");
ShunitResourcesMojo mojo = (ShunitResourcesMojo) lookupMojo("prepare",testPom);
assertNotNull(mojo);
mojo.execute();
}
mojo execute contains (and fails on)
Validate.notNull(project);
回答1:
I was just working on my own plugin and ran into the same problem. I could not find documentation on it, but I had a look at the maven-ant-plugin (https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-ant-plugin) to see how they did it.
I assume this is built-in test harness behavior, but you have to add a "project" parameter to your test pom.xml to set the value to a subclass of MavenProjectStub, for example in the "ant-test" test:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ant-plugin</artifactId>
<configuration>
<project implementation="org.apache.maven.plugin.ant.stubs.AntTestMavenProjectStub"/>
<settings implementation="org.apache.maven.plugin.ant.stubs.SettingsStub"/>
<localRepository>${localRepository}</localRepository>
</configuration>
</plugin>
I hope you had your answer already, just posting for those to follow.
来源:https://stackoverflow.com/questions/15512404/unit-testing-maven-mojo-components-and-parameters-are-null