具有多模块项目的JUnit类别 - 无法加载类别
问题描述:
我想要包含/排除JUnit分类测试。我在通用模块中定义了标记界面StressTest
。我在moduleA中引用StressTest
。我有以下根pom.xml
具有多模块项目的JUnit类别 - 无法加载类别
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<excludedGroups>com.mycompany.project.common.utils.StressTest</excludedGroups>
</configuration>
</plugin>
Maven的万无一失,插件在运行MVN测试,我得到同时建立另一个模块以下。
Unable to load category: com.mycompany.project.common.utils.StressTest
我应该在哪里写我的StressTest
接口?
答
为了确保'找到'您的@Category
类,它必须位于Maven项目的依赖关系树生成的类路径中。
此异常......
无法加载类:com.mycompany.project.common.utils.StressTest
...强烈暗示,无论神器包含com.mycompany.project.common.utils.StressTest
是不您的moduleA
声明的依赖关系。
因此,您需要在您的moduleA
上添加对任何工件包含com.mycompany.project.common.utils.StressTest
的依赖关系。如果这种依赖性的唯一目的是提供@Category
类,那么使这种依赖性测试的作用范围是有意义的,例如,
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>common.utils</artifactId>
<version>...</version>
<scope>test</scope>
</dependency>
此外,如果com.mycompany.project.common.utils.StressTest
在测试树,而不是主要树在您共同utils的模块,那么你将需要创建一个包含普通utils的模块的测试类的JAR。您可以通过添加以下maven-jar-plugin
声明共同utils的pom.xml中做到这一点:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
你会那么依赖于此的依赖性使用<type>test-jar</type>
在moduleA
,例如:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>common.utils</artifactId>
<version>...</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
如果我在测试/ java下定义StressTest,那么它不起作用,但是如果我在main/java中定义它,那么它就可以工作。 – bluetech
@bluetech我已经更新了答案,描述了如何导出并依赖于来自您的通用utils模块的'test-jar'。 – glytching