如何使用JUnit创建Maven插件的自动化测试?

Ste*_*yer 4 java automated-tests maven-2

我开发了一个(大部分)工作插件,但由于它的功能与它处理的项目直接相关,你如何为插件开发单元和集成测试.我最好的想法是为插件创建一个集成测试项目,该插件在其生命周期中使用插件,并且有测试报告插件在处理数据时的成功或失败.

谁有更好的想法?

Bri*_*ews 6

你需要使用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>

您从AbstractMojoTestCase派生单元测试类.

您需要创建一个裸骨POM,通常在src/test/resources文件夹中.

    <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>

使用AbstractMojoTest.lookupMojo(String,File)(或其他变体之一)为特定目标加载Mojo并执行它.

    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

我创建了自己的插件,你可以参考澄清http://ldap-plugin.btmatthews.com,