Introduction

Currently, Maven only supports unit testing out of the box. This document is intended to help Maven Developers to test Plugins with Unit Tests, Integration Tests or Functional tests.

Testing Styles: Unit Testing vs. Functional/Integration Testing

A unit test attempts to verify a mojo as an isolated unit, by mocking out the rest of the Maven environment. A mojo unit test does not attempt to run your plugin in the context of a real Maven build. Unit tests are designed to be fast.

A functional/integration test attempts to use a mojo in a real Maven build, by launching a real instance of Maven in a real project. Normally this requires you to construct special dummy Maven projects with real POM files. Often this requires you to have already installed your plugin into your local repository so it can be used in a real Maven build. Functional tests run much more slowly than unit tests, but they can catch bugs that you may not catch with unit tests.

The general wisdom is that your code should be mostly tested with unit tests, but should also have some functional tests.

Unit Tests

Using JUnit alone

In principle, you can write a unit test of a plugin Mojo the same way you'd write any other JUnit test case, by writing a class that extends TestCase.

However, most mojos need more information to work properly. For example, you'll probably need to inject a reference to a MavenProject, so your mojo can query project variables.

Using PlexusTestCase

Mojo variables are injected using Plexus, and many Mojos are written to take specific advantage of the Plexus container (by executing a lifecycle or having various injected dependencies).

If you all you need is Plexus container services, you can write your class with extends PlexusTestCase instead of TestCase.

With that said, if you need to inject Maven objects into your mojo, you'll probably prefer to use the maven-plugin-testing-harness.

maven-plugin-testing-harness

The maven-plugin-testing-harness is explicitly intended to test the org.apache.maven.reporting.AbstractMavenReport#execute() implementation.

In general, you need to include maven-plugin-testing-harness as dependency, and create a *MojoTest (by convention) class which extends AbstractMojoTestCase.

  1. ...
  2. <dependencies>
  3. ...
  4. <dependency>
  5. <groupId>org.apache.maven.plugin-testing</groupId>
  6. <artifactId>maven-plugin-testing-harness</artifactId>
  7. <version>3.3.0</version>
  8. <scope>test</scope>
  9. </dependency>
  10. ...
  11. </dependencies>
  12. ...
  1. public class YourMojoTest
  2. extends AbstractMojoTestCase
  3. {
  4. /**
  5. * @see junit.framework.TestCase#setUp()
  6. */
  7. protected void setUp() throws Exception
  8. {
  9. // required for mojo lookups to work
  10. super.setUp();
  11. }
  12.  
  13. /**
  14. * @throws Exception
  15. */
  16. public void testMojoGoal() throws Exception
  17. {
  18. File testPom = new File( getBasedir(),
  19. "src/test/resources/unit/basic-test/basic-test-plugin-config.xml" );
  20.  
  21. YourMojo mojo = (YourMojo) lookupMojo( "yourGoal", testPom );
  22.  
  23. assertNotNull( mojo );
  24. }
  25. }

For more information, please refer to Maven Plugin Harness Wiki

Integration/Functional testing

maven-verifier

maven-verifier tests are run using JUnit or TestNG, and provide a simple class allowing you to launch Maven and assert on its log file and built artifacts. It also provides a ResourceExtractor, which extracts a Maven project from your src/test/resources directory into a temporary working directory where you can do tricky stuff with it.

Maven itself uses maven-verifier to run its core integration tests. For more information, please refer to Creating a Maven Integration Test.

  1. public class TrivialMavenVerifierTest extends TestCase
  2. {
  3. public void testMyPlugin()
  4. throws Exception
  5. {
  6. // Check in your dummy Maven project in /src/test/resources/...
  7. // The testdir is computed from the location of this
  8. // file.
  9. File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/my-dummy-maven-project" );
  10.  
  11. Verifier verifier;
  12.  
  13. /*
  14. * We must first make sure that any artifact created
  15. * by this test has been removed from the local
  16. * repository. Failing to do this could cause
  17. * unstable test results. Fortunately, the verifier
  18. * makes it easy to do this.
  19. */
  20. verifier = new Verifier( testDir.getAbsolutePath() );
  21. verifier.deleteArtifact( "org.apache.maven.its.itsample", "parent", "1.0", "pom" );
  22. verifier.deleteArtifact( "org.apache.maven.its.itsample", "checkstyle-test", "1.0", "jar" );
  23. verifier.deleteArtifact( "org.apache.maven.its.itsample", "checkstyle-assembly", "1.0", "jar" );
  24.  
  25. /*
  26. * The Command Line Options (CLI) are passed to the
  27. * verifier as a list. This is handy for things like
  28. * redefining the local repository if needed. In
  29. * this case, we use the -N flag so that Maven won't
  30. * recurse. We are only installing the parent pom to
  31. * the local repo here.
  32. */
  33. List cliOptions = new ArrayList();
  34. cliOptions.add( "-N" );
  35. verifier.executeGoal( "install" );
  36.  
  37. /*
  38. * This is the simplest way to check a build
  39. * succeeded. It is also the simplest way to create
  40. * an IT test: make the build pass when the test
  41. * should pass, and make the build fail when the
  42. * test should fail. There are other methods
  43. * supported by the verifier. They can be seen here:
  44. * http://maven.apache.org/shared/maven-verifier/apidocs/index.html
  45. */
  46. verifier.verifyErrorFreeLog();
  47.  
  48. /*
  49. * Reset the streams before executing the verifier
  50. * again.
  51. */
  52. verifier.resetStreams();
  53.  
  54. /*
  55. * The verifier also supports beanshell scripts for
  56. * verification of more complex scenarios. There are
  57. * plenty of examples in the core-it tests here:
  58. * https://svn.apache.org/repos/asf/maven/core-integration-testing/trunk
  59. */
  60. }

Note: maven-verifier and maven-verifier-plugin sound similar, but are totally different unrelated pieces of code. maven-verifier-plugin simply verifies the existence/absence of files on the filesystem. You could use it for functional testing, but you may need more features than maven-verifier-plugin provides.

maven-invoker-plugin

You can use maven-invoker-plugin to invoke Maven and to provide some BeanShell/Groovy tests. Tests written in this way don't run under JUnit/TestNG; instead, they're run by Maven itself.

You can take a look at the maven-install-plugin how there are integration tests are written.

  1. <project
  2. xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. ...
  7. <build>
  8. <plugins>
  9. <plugin>
  10. <groupId>org.apache.maven.plugins</groupId>
  11. <artifactId>maven-invoker-plugin</artifactId>
  12. <version>1.10</version>
  13. <configuration>
  14. <projectsDirectory>src/it</projectsDirectory>
  15. <pomIncludes>
  16. <pomInclude>**/pom.xml</pomInclude>
  17. </pomIncludes>
  18. <postBuildHookScript>verify</postBuildHookScript>
  19. </configuration>
  20. <executions>
  21. <execution>
  22. <goals>
  23. <goal>run</goal>
  24. </goals>
  25. </execution>
  26. </executions>
  27. </plugin>
  28. ...
  29. </plugins>
  30. </build>
  31. ...
  32. </project>