------ How To Use Maven Plugin Testing Harness ------ Vincent Siveton ------ 2008-08-27 ------ ~~ 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. Cookbook: How To Use Maven Plugin Testing Harness? This guide is intended as a reference for those developing Maven plugins, with self-contained references and solutions for common testing cases. * Prerequisites We assume that you have already created a plugin. In this cookbook, we make reference to <<>> in <<>> which is generated by the Maven Archetype Plugin, i.e.: +---+ mvn archetype:create \ -DgroupId=org.apache.maven.plugin.my \ -DartifactId=maven-my-plugin \ -DarchetypeArtifactId=maven-archetype-mojo +---+ The generated structure should be: +---+ maven-my-plugin |- pom.xml +- src/ +- main/ +- java/ +- org/ +- apache/ +- maven/ +- plugin/ +- my/ |- MyMojo.java +---+ * Recipe ** Add <<>> dependency As usual, just add <<>> as following in your pom. Be sure to specify <<>> scope. +---+ ... org.apache.maven.plugin-testing maven-plugin-testing-harness test ... ... +---+ ** Create a <<>> Create a <<>> (by convention) class in <<>> directory. This class should extend <<>> from <<>>. +---+ import org.apache.maven.plugin.testing.AbstractMojoTestCase; public class MyMojoTest extends AbstractMojoTestCase { /** {@inheritDoc} */ protected void setUp() throws Exception { // required super.setUp(); ... } /** {@inheritDoc} */ protected void tearDown() throws Exception { // required super.tearDown(); ... } /** * @throws Exception if any */ public void testSomething() throws Exception { File pom = getTestFile( "src/test/resources/unit/project-to-test/pom.xml" ); assertNotNull( pom ); assertTrue( pom.exists() ); MyMojo myMojo = (MyMojo) lookupMojo( "touch", pom ); assertNotNull( myMojo ); myMojo.execute(); ... } } +---+ In this case, <<>> will test <<>> against a Maven project called <<>>. <>: By convention, projects for unit testing your should be in the test resources directory. Alternatively to extending <<>> and when using Junit-4.10 ff., you may use a <<>>, which just embeds an <<>>. When you do not need the functionality in every test method, you may use the <<<@WithoutMojo>>> annotation to skip rule executions. +---+ import org.apache.maven.plugin.testing.MojoRule; import org.apache.maven.plugin.testing.WithoutMojo; import org.junit.Rule; import static org.junit.Assert.*; import org.junit.Test; public class MyMojoTest { @Rule public MojoRule rule = new MojoRule() { @Override protected void before() throws Throwable { } @Override protected void after() { } }; /** * @throws Exception if any */ @Test public void testSomething() throws Exception { File pom = rule.getTestFile( "src/test/resources/unit/project-to-test/pom.xml" ); assertNotNull( pom ); assertTrue( pom.exists() ); MyMojo myMojo = (MyMojo) rule.lookupMojo( "touch", pom ); assertNotNull( myMojo ); myMojo.execute(); ... } /** Do not need the MojoRule. */ @WithoutMojo @Test public void testSomethingWhichDoesNotNeedTheMojoAndProbablyShouldBeExtractedIntoANewClassOfItsOwn() { ... } } +---+ ** Configuring <<>> pom Just create a pom as usual. The names for groupId and artifactId don't really matter since this project will not be deployed. +---+ 4.0.0 org.apache.maven.plugin.my.unit project-to-test 1.0-SNAPSHOT jar Test MyMojo junit junit 3.8.1 test maven-my-plugin target/test-harness/project-to-test +---+ ** Execute test As usual, just call: +---+ mvn test +---+ * Resources [[1]] {{{http://maven.apache.org/guides/plugin/guide-java-plugin-development.html}Guide to Developing Java Plugins}} [[2]] {{{http://maven.apache.org/guides/mini/guide-configuring-plugins.html}Guide to Configuring Plugins}} []