Maven Plugin Testing - In a Modern way - Part V
Tags: programming, java, maven, testing
Published: 2020-09-10 17:40:51

---
title: "Maven Plugin Testing - In a Modern way - Part V"
published: true
description: 
tags: programming,java,maven,testing
series: Maven Plugin Testing - In a Modern way
---
In the [previous part of the series - Maven Plugin Testing - In a Modern way - Part IV](https://dev.to/khmarbaise/maven-plugin-testing-in-a-modern-way-part-iv-4ko0) we have seen how to define goals to run Maven. In this part we will take a deeper look how we can define system properties for a Maven call to be used.

Let us take a look at a simple example taken from the previous part.
```java
@MavenJupiterExtension
class BaseIT {

  @MavenTest
  @MavenGoal("verify")
  void the_first_test_case(MavenExecutionResult result) {
     ...
  }
}
```
This will run Maven with the goal `verify`. So what could you do to prevent running the unit tests or even any tests at all within that integration test case? What would you usually do on plain command line? You would have added `-DskipTests` to your Maven command line. How could you do the above integration test example? Very easy like this:
```java
@MavenJupiterExtension
class PropertyIT {

  @MavenTest
  @MavenGoal("verify")
  @SystemProperty("skipTests")
  void goal_clean_skiptests (MavenExecutionResult result) {
     ...
  }
}
```
That means in the end that Maven will be called like the following:
```
mvn -DskipTests verify
```
Let us go a step further and see how to write a test case for a plugin which should be called like this:
```
mvn ...:failure -DexecutionException=true -Dexception="This is the value of exception."
``` 
The above could be expressed like this:
```java
@MavenJupiterExtension
class PropertyIT {

  @MavenTest
  @MavenGoal("clean")
  @MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:failure")
  @SystemProperty(value = "executionException", content = "true")
  @SystemProperty(value = "exception", content = "This is the value of exception.")
  void goal_failure_execution(MavenExecutionResult result) {
     ...
  }
}
```
You can see two things here. First you can repeat the `@SystemProperty` to define multiple system properties and second you can make a clear separation between the name and property value.

As you might already expected you can create a meta annotation where you can combine the properties used in the previous example into this single meta annotation: 
```java
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RUNTIME)
@Inherited
@SystemProperty(value = "executionException", content = "true")
@SystemProperty(value = "exception", content = "This is the value of exception.")
public @interface ExecutionException {
}
```
If reconsider this it's thought a bit too short, because that can be made better like this:
```java
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RUNTIME)
@Inherited
@MavenTest
@MavenGoal("clean")
@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:failure")
@SystemProperty(value = "executionException", content = "true")
@SystemProperty(value = "exception", content = "This is the value of exception.")
public @interface FailureGoalWithExecutionException {
}
```
So the defined meta annotation can now being used to make the test looking like this:
```java
@MavenJupiterExtension
class PropertyIT {

  @FailureGoalWithExecutionException
  void goal_failure_execution(MavenExecutionResult result) {
     ...
  }
}
```
You can define the previous described meta annotation in the [example project][example-v] and see how it feels to use it.

Based on convenience you can of course create a meta annotation like this:
```java
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RUNTIME)
@Inherited
@SystemProperty("skipTests")
public @interface MavenSkipTests {
}
```
or other things. If you have good ideas do not hesitate to file in a [feature request][issue] for the project so it can be made permanent part of it.

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

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

An example project which shows the previous example can be [found on GitHub][example-v].

[issue]: https://github.com/khmarbaise/maven-it-extension/issues
[users-guide]: https://khmarbaise.github.io/maven-it-extension/itf-documentation/usersguide/usersguide.html
[release-notes]: https://khmarbaise.github.io/maven-it-extension/itf-documentation/release-notes/release-notes.html
[background]: https://khmarbaise.github.io/maven-it-extension/itf-documentation/background/background.html
[itf]: https://github.com/khmarbaise/maven-it-extension
[junit-jupiter]: https://junit.org/junit5/docs/current/user-guide/
[junit-jupiter-extension]: https://junit.org/junit5/docs/current/user-guide/#extensions
[assertj]: https://assertj.github.io/doc/
[maven-failsafe-plugin]: https://maven.apache.org/surefire/maven-failsafe-plugin/index.html
[maven]: https://maven.apache.org
[containssubsequence]: https://www.javadoc.io/doc/org.assertj/assertj-core/latest/org/assertj/core/api/ListAssert.html#containsSubsequence(ELEMENT...)
[mavenexecutionresult]: https://javadoc.io/doc/com.soebes.itf.jupiter.extension/itf-extension-maven/latest/com/soebes/itf/jupiter/maven/MavenExecutionResult.html
[example-v]: https://github.com/khmarbaise/itf-example-article-part-v
[lifecycle]: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html