I'm looking for information about how to write unit tests for Maven plugins. Although there's a page on the Maven site about this topic, there's only one example unit test, which does nothing useful. I also found this wiki page, but it hasn't been updated for more than 4 years, so I'm reluctant to invest any faith in it.
I checked the book "The Definitive Guide to Maven", but it doesn't even mention the subject. I'd like to write my unit tests in either Groovy or Java, and would appreciate any information about how to do this.
I'm using Maven version 2.1.0.
Running Unit Tests With the Maven Surefire Plugin If we use the default configuration, the Maven Surefire plugin runs all test methods found from the test classes that fulfill these two conditions: The test classes must be found from the src/test/java directory.
Requirements: Maven 3.2. 5 and JDK 1.8 or higher. The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application.
We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.
JUnit HTML Reports Maven surefire plugin generates text and XML reports, we can generate HTML based reports using maven-surefire-report-plugin . Below configuration works for both JUnit 4 and JUnit 5. Just run mvn site command and the HTML report will be generated in the target/site/ directory.
The first link you posted is the right place to learn about the various possibilities and to start with. But for examples, the best thing to do would be to look at the sources of existing plugins. Having that said:
For unit testing, my preference goes to the maven-plugin-testing-harness
. You can learn (a bit) more about it in the Maven Plugin Harness Wiki but, as I said, especially in plugins using it, for example the maven-compiler-plugin
. Check the CompilerMojoTestCase
.
For integration testing (it's IMO very important for plugins to do integration testing), I'd recommend the shitty-maven-plugin
(Super Helpful Integration Testing ThingY), especially if you are interested by writing tests in Groovy, or the maven-invoker-plugin
. They are similar but have both features that the other doesn't. The website of the SHITTY plugin mentions several plugins using it. Check it out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With