这是我的测试(maven-plugin-test-harness 3.3.0,junit 5.6.2):
import java.io.File;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public final class MyMojoTest extends AbstractMojoTestCase {
@BeforeEach
public void setup() throws Exception {
this.setUp();
}
@Test
public void executeIt() throws Exception {
final File pom = new File("src/test/resources/my-test-pom.xml");
final MyMojo mojo = MyMojo.class.cast(
this.lookupMojo("mygoal", pom)
);
mojo.execute();
}
}
这是我在MyMojo
中拥有的(maven-plugin-api 3.8.4):
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
@Mojo(name = "my", defaultPhase = LifecyclePhase.COMPILE)
public final class MyMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject project;
}
问题是lookupMojo()
返回的mojo
没有设置project
属性(它是null
).
有人提出了一些解决方案here,但我不确定它如何与JUnit5一起工作.