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

时间:2022-09-07 15:46:26

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?

谁有更好的想法?

2 个解决方案

#1


6  

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

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

您从AbstractMojoTestCase派生单元测试类。

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

您需要创建一个裸骨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>

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

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

I created my a plugin of my own that you can refer to for clarification http://ldap-plugin.btmatthews.com,

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

#2


1  

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.

如果你想看一些真实的例子,Terracotta Maven插件(tc-maven-plugin)有一些测试,你可以在开源伪造中仔细阅读。

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

该插件位于: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/

源代码见svn: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/

在该源代码中,您可以在以下位置找到一些实际的Maven插件测试:src / test / java / org / terracotta / maven / plugins / tc /

#1


6  

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

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

您从AbstractMojoTestCase派生单元测试类。

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

您需要创建一个裸骨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>

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

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

I created my a plugin of my own that you can refer to for clarification http://ldap-plugin.btmatthews.com,

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

#2


1  

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.

如果你想看一些真实的例子,Terracotta Maven插件(tc-maven-plugin)有一些测试,你可以在开源伪造中仔细阅读。

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

该插件位于: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/

源代码见svn: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/

在该源代码中,您可以在以下位置找到一些实际的Maven插件测试:src / test / java / org / terracotta / maven / plugins / tc /