Issue
This is my test (maven-plugin-testing-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();
}
}
This is what I have in 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;
}
The problem is that mojo
returned by lookupMojo()
doesn't have the project
attribute set (it's null
).
Some solution was proposed here, but I'm not sure how it can work with JUnit 5.
Solution
I tried with the same configurations as mentioned above. The plugin works fine but none of the tests having lookupMojo()
seems to be working.
A similar test example can be referred here. There is a difference in the setUp
method from your class MyMojoTest
and example provided in the link.
super.setUp();
should be called instead of this.setUp()
so as to initialize all the objects in AbstractMojoTestCase
class.
The possible reason that the test case with maven-plugin-testing-harness 3.3.0
and junit 5.6.2
will not work because they are not compatible
.
The reasons are
maven-plugin-testing-harness
was built to be compatible withJunit4
. Latest update was a long time ago i.e. Dec 17, 2014.Junit 4
andJunit 5
are not compatible. We have to make use ofJunit-Vintage-Engine
to make it work.maven-plugin-testing-harness
was develop usingJDk-7
and minimum requirements forJunit 5
isJdk-8
. Information from theharness
plugin Manifest file
Implementation-Vendor-Id: org.apache.maven.plugin-testing
Built-By: igor
Build-Jdk: 1.7.0_55
Specification-Vendor: The Apache Software
Foundation Specification-Title: Maven Plugin Testing Mechanism
- Maven version supported is also different for both the jars. link
Few other links confirm the same.
There are very few libraries and informational link available for plugin testing with Junit5. I could find only a handful of them, although I haven't tried them yet.
Library:
<dependency>
<groupId>com.soebes.itf.jupiter.extension</groupId>
<artifactId>itf-assertj</artifactId>
<version>0.11.0</version>
<scope>test</scope>
</dependency>
Few more Jupiter extension libraries in this link
Examples related to it.
Answered By - Tris
Answer Checked By - Katrina (JavaFixing Volunteer)