DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • 7 Awesome Libraries for Java Unit and Integration Testing
  • The Most Popular Technologies for Java Microservices Right Now
  • Micronaut With Relation Database and...Tests
  • Unit Testing and Integration Testing in Practice

Trending

  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • AI, ML, and Data Science: Shaping the Future of Automation
  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Maven Plugin Testing - in a Modern Way - Part I

Maven Plugin Testing - in a Modern Way - Part I

By 
Karl Heinz Marbaise user avatar
Karl Heinz Marbaise
·
Aug. 31, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
8.4K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • Maven Invoker Plugin
  • Maven Verifier
  • Maven Plugin Testing Harness
  • etc.

You can read about different reasons 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:

Java
xxxxxxxxxx
1
 
1
@MavenJupiterExtension
2
class FirstMavenIT {
3
4
  @MavenTest
5
  void the_first_test_case(MavenExecutionResult result) {
6
    assertThat(result).build().isSuccessful();
7
  }
8
9
}


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 tests like src/test/java/.... I recommend to name your integration tests like *IT.java but of course you can change that if you like.

Plain Text
xxxxxxxxxx
1
 
1
.
2
└── src/
3
    └── test/
4
        └── java/
5
            └── org/
6
                └── it/
7
                    └── FirstMavenIT.java


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

Plain Text
xxxxxxxxxx
1
10
 
1
.
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 comprises 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:

XML
xxxxxxxxxx
1
18
 
1
<build>
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>
18
</build>


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.

XML
xxxxxxxxxx
1
28
 
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:

XML
xxxxxxxxxx
1
10
 
1
<testResources>
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>
10
</testResources>


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 location. So now we need to go for the itf-maven-plugin like this:

XML
xxxxxxxxxx
1
14
 
1
<plugin>
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>
14
</plugin>


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:

XML
xxxxxxxxxx
1
18
 
1
<plugin>
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>
17
</executions>
18
</plugin>


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

Shell
xxxxxxxxxx
1
 
1
mvn clean verify


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

Plain Text
x
15
 
1
.
2
└──target/
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.

Apache Maven unit test Integration integration test Integration testing Directory Plain text

Opinions expressed by DZone contributors are their own.

Related

  • 7 Awesome Libraries for Java Unit and Integration Testing
  • The Most Popular Technologies for Java Microservices Right Now
  • Micronaut With Relation Database and...Tests
  • Unit Testing and Integration Testing in Practice

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!