Maven Plugin Testing - In a Modern way - Part I

You have decided to write a Maven Plugin. Now the important part comes around the corner: How to test a Maven Plugin? There are in general several options to test a Maven Plugins for example:

You can read about different reason not to use one of those options.

Coming back to the subject and think about to write integration tests for a Maven plugin. Writing integration test means having three parties involved:

  1. The component you would like to test (typically the Maven plugin/Extension etc.).
  2. The testing code by which you check the functionality.
  3. The Project you would like to test with (where your Maven plugin usually is configured in to be used.)

Let us start with a simple test case which simply requires that the build which contains your new plugin to run successful. This looks like this by using the Integration Testing Framework:

2class FirstMavenIT {
4  @MavenTest
5  void the_first_test_case(MavenExecutionResult result) {
6    assertThat(result).build().isSuccessful();
7  }

The written tests is easy to understand? Isn't it? The foundation of the Integration Testing Framework is the JUnit Jupiter extension mechanism which allows to easily write extensions which can do many things.

Going back to the given simple integration test we need now two other parts to get a working integration test. The code of the plugin (which I assume already exists) and of course the project where you would like to test your plugin with.

The above is an integration test written by using the Integration Testing Framework (ITF for short) which exactly does that.

So a bit more nifty details here. The @MavenJupiterExtension is the annotation to activate the ITF extension. The @MavenTest is the equivalent for unit tests which are annotated with @Test of course it makes it very clear that we are in the context of a test for Maven.

Ah finally we have the parameter of the test method MavenExecutionResult result which gives you access to the result of the build and many more. The assertThat(..) is a customer assertions for AssertJ to make writing assertions more convenient.

So now we have to define the project which is used to use our plugin. As always in Apache Maven you should follow convention over configuration and the ITF is not an exception of that. We locate the integration itself into the usual location of test like src/test/java/.... I recommend to name your integration tests like * but of course you can change that if you like.

2└── src/
3    └── test/
4        └── java/
5            └── org/
6                └── it/
7                    └──

So now we need to put the project into a particular location like this:

 2└── src/
 3    └── test/
 4        └── resources-its/
 5            └── org/
 6                └── it/
 7                    └── FirstMavenIT/
 8                        └── the_first_test_case/
 9                            β”œβ”€β”€ src/
10                            └── pom.xml

It is important to mention that the directory FirstMavenIT represents the integration test class and the the_first_test_case directory represents the method name of the integration test class. This will give us the opportunity to define several test cases within the class. Based on the association between method name and directory name it is necessary to write method names in lowercase and separate by using an underscore. If you use camel case method names this could cause issues with case insensitive file systems.

As you can see that the directory the_first_test_case contains a full fledged project which comprise of a pom.xml file and of course the usual structures for source code and maybe unit test etc.

Let us take a look into the pom.xml of the project which is used to test our plugin:

 2  <plugins>
 3    <plugin>
 4      <groupId>com.soebes.itf.jupiter.extension</groupId>
 5      <artifactId>itf-failure-plugin</artifactId>
 6      <version>@project.version@</version>
 7      <executions>
 8        <execution>
 9          <id>the_first_test_case</id>
10          <phase>initialize</phase>
11          <goals>
12            <goal>failure</goal>
13          </goals>
14        </execution>
15      </executions>
16    </plugin>
17  </plugins>

One interesting thing is important here: @project.version@ which represents the current version of your plugin project.

There are some requirements to use the ITF:

  • JDK 8+
  • Apache Maven 3.1.0 or above.

To get the integration tests running we need to add some parts to the projects pom.xml which is at first the dependencies to ITF:

The dependency com.soebes.itf.jupiter.extension:itf-assertj contains custom assertions of AssertJ in case you want to use AssertJ as your assertion framework. This means you have to include org.assertj:assertj-core as well. If you don’t want to use AssertJ as assertion framework you can omit them both.

 1    <dependency>
 2      <groupId>com.soebes.itf.jupiter.extension</groupId>
 3      <artifactId>itf-extension-maven</artifactId>
 4      <version>0.9.0</version>
 5      <scope>test</scope>
 6    </dependency>
 7    <dependency>
 8      <groupId>org.junit.jupiter</groupId>
 9      <artifactId>junit-jupiter-engine</artifactId>
10      <scope>test</scope>
11    </dependency>
12    <dependency>
13      <groupId>com.soebes.itf.jupiter.extension</groupId>
14      <artifactId>itf-assertj</artifactId>
15      <version>0.9.0</version>
16      <scope>test</scope>
17    </dependency>
18    <dependency>
19      <groupId>com.soebes.itf.jupiter.extension</groupId>
20      <artifactId>itf-jupiter-extension</artifactId>
21      <version>0.9.0</version>
22      <scope>test</scope>
23    </dependency>
24    <dependency>
25      <groupId>org.assertj</groupId>
26      <artifactId>assertj-core</artifactId>
27      <scope>test</scope>
28    </dependency>

You need to add the resource filtering like this:

 2  <testResource>
 3    <directory>src/test/resources</directory>
 4    <filtering>false</filtering>
 5  </testResource>
 6  <testResource>
 7    <directory>src/test/resources-its</directory>
 8    <filtering>true</filtering>
 9  </testResource>

The first one for src/test/resources might be change based on your own requirements. The second is needed to copy the test projects to the appropriate locations. So now we need to go for the itf-maven-plugin like this:

 2  <groupId>com.soebes.itf.jupiter.extension</groupId>
 3  <artifactId>itf-maven-plugin</artifactId>
 4  <version>0.9.0</version>
 5  <executions>
 6    <execution>
 7      <id>installing</id>
 8      <phase>pre-integration-test</phase>
 9      <goals>
10        <goal>install</goal>
11      </goals>
12    </execution>
13  </executions>

which is responsible to copy your plugin/extension to the correct location and finally the maven-failsafe-plugin to execute the integration tests like this:

 2  <groupId>org.apache.maven.plugins</groupId>
 3  <artifactId>maven-failsafe-plugin</artifactId>
 4  <configuration>
 5    <systemProperties>
 6      <maven.version>${maven.version}</maven.version>
 7      <maven.home>${maven.home}</maven.home>
 8    </systemProperties>
 9  </configuration>
10  <executions>
11  <execution>
12    <goals>
13      <goal>integration-test</goal>
14      <goal>verify</goal>
15    </goals>
16  </execution>

So now you can run the integration tests simply by using:

1mvn clean verify

After the execution you will find a directory a structure like this:

 3   └── maven-it/
 4       └── org/
 5           └── it/
 6               └── FirstMavenIT/
 7                   └── the_first_test_case/
 8                       β”œβ”€β”€ .m2/
 9                       β”œβ”€β”€ project/
10                       β”‚   β”œβ”€β”€ src/
11                       β”‚   β”œβ”€β”€ target/
12                       β”‚   └── pom.xml
13                       β”œβ”€β”€ mvn-stdout.log
14                       β”œβ”€β”€ mvn-stderr.log
15                       └── mvn-arguments.log

The directory the_first_test_case represents the test case from our original integration test class FirstMavenIT. The project which has been used to test the plugin is put into the directory project which also contains the target folder after a run of such project. The directory project is the location where you can go into and start manually Maven by using the command line arguments which are stored in mvn-arguments.log. The result of this execution is redirected into mvn-stdout.log and the redirection of stderr is logged into mvn-stderr.log. The directory .m2/repository contains the local cache (aka maven repository) of that build.

So this it is for Part I. If you like to learn more about the Integration Testing Framework you can consult the users guide. If you like to know the state of the release you can take a look into the release notes.

If you have ideas, suggestions or found bugs please file in an issue on github.

A full example can be found here: