How do you create automated Maven plugin tests using JUnit?

I have a (mostly) working plugin, but since its function is directly related to the project that it processes, how do you develop unit and integration tests for the plugin. The best idea I got is to create an integration project for a plugin that uses the plugin throughout its life cycle and has tests that report plugin successes or data processing failures.

Anyone with better ideas?

+4
source share
2 answers

You need to use maven-plugin-testing-harness ,

  <dependency>
         <groupId> org.apache.maven.shared </groupId>
         <artifactId> maven-plugin-testing-harness </artifactId>
         <version> 1.1 </version>
         <scope> test </scope>
     </dependency>

You get unit test classes from AbstractMojoTestCase .

You need to create a POM with bare bones, usually in the src/test/resources folder.

  <project>
         <build>
             <plugins>
                 <plugin>
                     <groupId> com.mydomain, mytools </groupId>
                     <artifactId> mytool-maven-plugin </artifactId>
                     <configuration>
                         <! - Insert configuration settings here ->
                     </configuration>
                     <executions>
                         <execution>
                             <goals>
                                 <goal> mygoal </goal>
                             </goals>
                         </execution>
                     </executions>
                 </plugin>
             </plugins>
         </build>
     </project>

Use AbstractMojoTest.lookupMojo (String, File) (or one of the other options) to load Mojo for a specific purpose and execute it.

  final File testPom = new File (PlexusTestCase.getBasedir (), "/target/test-classes/mytools-plugin-config.xml");
     Mojo mojo = this.lookupMojo ("mygoal", testPom);
     // Insert assertions to validate that your plugin was initialised correctly
     mojo.execute ();
     // Insert assertions to validate that your plugin behaved as expected

I created my own plugin, which you can find for explanation at http://ldap-plugin.btmatthews.com ,

+6
source

If you want to see some real world examples, the Terracotta Maven plugin (tc-maven-plugin) has several tests with it that you can view in openge forge.

The plugin is located at: http://forge.terracotta.org/releases/projects/tc-maven-plugin/

And the source is in svn at: http://svn.terracotta.org/svn/forge/projects/tc-maven-plugin/trunk/

And in this source you can find some actual Maven plugin tests at: src / test / java / org / terracotta / maven / plugins / tc /

+1
source

Source: https://habr.com/ru/post/1276955/


All Articles