我使用代码生成器JAVA程序生成单元测试源到targetgenerated-test-sources
,我成功地编译到targettest-classes
,但它们被maven构建忽略了。我用mvn clean test
启动这个构建。
在我的pom.xml文件中,我有一个主类com.mypackage.TestCaseGenerator
的执行目标,我绑定到generate-test-sources
阶段。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>myexec</id>
<phase>generate-test-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<classpathScope>test</classpathScope>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>com.mypackage.TestCaseGenerator</argument>
<argument>target/generated-test-sources/somedir</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-test-sources/somedir</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
当我从Eclipse中使用right-click -> run as JUnit Test
在target/generated-test-sources/somedir
上运行测试用例时,测试执行正确。
我是否缺少一些maven配置?
如果您生成的测试的类/文件名与Maven Surefire Plugin的默认值不匹配,您将不得不配置它以包含其他模式的测试。
请参阅官方插件文档了解测试的包含和排除:
默认情况下,Surefire Plugin将自动包含所有带有以下通配符模式的测试类:
"**/Test*.java"
-包括其所有子目录和所有以"Test"开头的Java文件名。"**/*Test.java"
-包括其所有子目录和所有以"Test"结尾的Java文件名。"**/*Tests.java"
-包括其所有子目录和所有以"Tests"结尾的Java文件名。"**/*TestCase.java"
-包括其所有子目录和所有以"TestCase"结尾的Java文件名。
您可以使用插件配置的<includes>
或<excludes>
部分配置不同的模式。你可以在上面链接的文档页面找到如何做到这一点的好例子。