如何使用JUnit创建Maven插件的自动化测试?
-
01-07-2019 - |
题
我开发了一个(大部分)工作插件,但由于它的功能与它处理的项目直接相关,你如何为插件开发单元和集成测试。我最好的想法是为插件创建一个集成测试项目,该插件在其生命周期中使用插件,并且有测试报告插件在处理数据时的成功或失败。
任何有更好想法的人?
解决方案
您需要使用 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 ,
其他提示
如果你想看一些真实的例子,Terracotta Maven插件(tc-maven-plugin)有一些测试,你可以在开源伪造中仔细阅读。
该插件位于: http://forge.terracotta.org /释放/项目/ TC-行家-插件/
源代码位于以下网址: http: //svn.terracotta.org/svn/forge/projects/tc-maven-plugin/trunk/
在该源代码中,您可以在以下位置找到一些实际的Maven插件测试:src / test / java / org / terracotta / maven / plugins / tc /
不隶属于 StackOverflow