-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MPLUGINTESTING-93] Documentation for Plugin testing #597
Open
aamotharald
wants to merge
13
commits into
apache:master
Choose a base branch
from
aamotharald:docu/MPLUGINTESTING-93
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
83d1a18
conversion from apt to markdown for plugin-testing
aamotharald bbbe2fd
manual markdown adoption and JUnit5 testing docu
aamotharald bfea506
Merge remote-tracking branch 'origin/master' into docu/MPLUGINTESTING-93
aamotharald 9d136d0
eprecated JUnit4 style tests docu adoption
aamotharald 0573f04
Merge branch 'apache:master' into docu/MPLUGINTESTING-93
aamotharald ec31f18
review comments from Bukama, slawekjaranowski and elharo
aamotharald 2175392
further review comments from Bukama
aamotharald ddf1fed
Merge branch 'apache:master' into docu/MPLUGINTESTING-93
aamotharald 4056ce2
review comments from elharo
aamotharald 3083bd0
further adoptions for comments from elharo
aamotharald 8cc434d
parentheses around injectMojo
aamotharald 668d60f
further review from elharo
aamotharald 6085d99
Merge branch 'apache:master' into docu/MPLUGINTESTING-93
aamotharald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
--> | ||
# Developers Centre - Testing Plugins | ||
|
||
## Introduction | ||
|
||
This document is intended to help Maven Developers test plugins with unit tests, integration tests, and 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. | ||
|
||
Functional tests run much more slowly than unit tests, but they can catch bugs or detect issues 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. | ||
|
||
However, many Mojo methods need more dependencies 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 by Guice (an open-source software framework for the Java platform), sometimes with a Codehaus Plexus (a collection of components used by Apache Maven) adapter. | ||
|
||
Both Guice-based and Plexus-based Mojos inject dependencies by having the test class extend `PlexusTestCase` and calling the **lookup()** method to instantiate the Mojo. | ||
Tests for fully guicified Mojos can also inject dependencies directly into the constructor without extending `PlexusTestCase`. | ||
These dependencies can be Mockito mocks or instances of the actual model classes. | ||
If a particular test does not access the injected field — that is, it’s only injected to fulfill the constructor signature — you can usually pass null as the value of that argument. | ||
|
||
With that said, if you need to inject Maven objects into your Mojo, you’ll probably prefer to use the maven-plugin-testing-harness. | ||
|
||
### Using the maven-plugin-testing-harness | ||
|
||
The [maven-plugin-testing-harness](/plugin-testing/maven-plugin-testing-harness/) is designed to test the implementation of the `org.apache.maven.reporting.AbstractMavenReport#execute()` method. | ||
|
||
You have to include `maven-plugin-testing-harness` as a test-scoped dependency. | ||
|
||
```xml | ||
... | ||
<dependencies> | ||
... | ||
<dependency> | ||
<groupId>org.apache.maven.plugin-testing</groupId> | ||
<artifactId>maven-plugin-testing-harness</artifactId> | ||
<!-- use the latest version of the maven-plugin-testing-harness, >= 3.4.0 --> | ||
aamotharald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<version>3.4.0</version> | ||
aamotharald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<scope>test</scope> | ||
</dependency> | ||
... | ||
</dependencies> | ||
... | ||
``` | ||
|
||
#### JUnit Jupiter (JUnit 5) style tests | ||
|
||
JUnit Jupiter uses an extension framework for which `MojoExtension` is provided by the `maven-plugin-testing-harness`. | ||
You can annotate your JUnit Jupiter test with `@MojoTest`; then inject the Mojo under test into the test method with the `@InjectMojo` annotation. | ||
This functionality was introduced in version `3.4.0` of the `maven-plugin-testing-harness`. | ||
Below is an example: | ||
|
||
```java | ||
@MojoTest | ||
public class YourMojoTest { | ||
|
||
private static final String POM = "src/test/resources/unit/basic-test/basic-test-plugin-config.xml"; | ||
aamotharald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Test | ||
@InjectMojo(goal = "generate", pom = POM) | ||
void simpleMojo(YourMojo mojo) { | ||
assertNotNull(mojo); | ||
} | ||
} | ||
``` | ||
|
||
#### JUnit 4 style tests (deprecated) | ||
For Maven 3 there is the deprecated way to write tests using JUnit 4 style. | ||
aamotharald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
For Maven 4 only JUnit Jupiter style tests are available. | ||
JUnit 4 is no longer supported. | ||
|
||
## Integration/Functional testing | ||
|
||
### Maven Verifier Component | ||
|
||
The Apache Maven Verifier Component (maven-verifier) tests are run using JUnit. | ||
It provides a simple class allowing you to launch Maven and assert on its log file and its build artifacts. | ||
It also provides a `ResourceExtractor`, which extracts a Maven project from the src/test/resources directory into a temporary working directory where you can do tricky stuff with it. | ||
aamotharald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Follow the [Getting Started](/shared/maven-verifier/getting-started.html) guide to learn more about creating maven-verifier tests. | ||
|
||
Maven itself uses maven-verifier to run its core integration tests. | ||
For more information, see [Creating a Maven Integration Test](https://cwiki.apache.org/confluence/display/MAVEN/Creating+a+Maven+Integration+Test). | ||
|
||
**Note**: maven-verifier and maven-verifier-plugin sound similar, but are totally different and unrelated pieces of code. | ||
The 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 the maven-verifier-plugin provides. | ||
|
||
### maven-invoker-plugin | ||
|
||
You can use the [maven-invoker-plugin](https://maven.apache.org/plugins/maven-invoker-plugin/) to invoke Maven and to provide some BeanShell/Groovy tests. | ||
Tests written in this way don’t run with a testing framework. | ||
Instead, they’re run by Maven itself. | ||
|
||
You can take a look at the [maven-install-plugin](https://github.com/apache/maven-install-plugin/tree/master/src/it) to see how integration tests are written. | ||
|
||
```xml | ||
... | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-invoker-plugin</artifactId> | ||
<version>1.10</version> | ||
<configuration> | ||
<projectsDirectory>src/it</projectsDirectory> | ||
<pomIncludes> | ||
<pomInclude>**/pom.xml</pomInclude> | ||
</pomIncludes> | ||
<postBuildHookScript>verify</postBuildHookScript> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>run</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
... | ||
</plugins> | ||
</build> | ||
... | ||
``` | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete "(an open-source software framework for the Java platform)" as it doesn't add much. Folks can Google it if they've never heard of it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and might as wellm delete "(a collection of components used by Apache Maven)" too