Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unit-testing maven plugins

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.

like image 962
Dónal Avatar asked May 05 '10 11:05

Dónal


People also ask

What is unit testing in Maven?

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.

Which plugin is used in Maven to run test cases?

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.

How Maven run test cases?

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.

How can Junits be implemented using Maven?

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.


1 Answers

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.

like image 140
Pascal Thivent Avatar answered Sep 30 '22 18:09

Pascal Thivent