Question

I've got a (mostly) working plugin developed, but since its function is directly related to the project it processes, how do you develop unit and integration tests for the plugin. The best idea I've had is to create an integration test project for the plugin that uses the plugin during its lifecycle and has tests that report on the plugins success or failure in processing the data.

Anyone with better ideas?

Was it helpful?

Solution

You need to use the 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 derive your unit test classes from AbstractMojoTestCase.

You need to create a bare bones POM, 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 the AbstractMojoTest.lookupMojo(String,File) (or one of the other variations) to load the Mojo for a specific goal 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 a plugin of my own that you can refer to for clarification http://ldap-plugin.btmatthews.com,

OTHER TIPS

If you'd like to see some real-world examples, the Terracotta Maven plugin (tc-maven-plugin) has some tests with it that you can peruse in the open source forge.

The plugin is 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 that source you can find some actual Maven plugin tests at: src/test/java/org/terracotta/maven/plugins/tc/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top