Java 具有多模块项目的JUnit类别-无法加载类别
我想包括/排除JUnit分类测试。我在一个通用模块中定义了一个标记接口Java 具有多模块项目的JUnit类别-无法加载类别,java,maven,unit-testing,testing,junit4,Java,Maven,Unit Testing,Testing,Junit4,我想包括/排除JUnit分类测试。我在一个通用模块中定义了一个标记接口StressTest。我在模块A中引用了StressTest。我在rootpom.xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <
StressTest
。我在模块A中引用了StressTest
。我在rootpom.xml
<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>
我应该在哪里编写我的
StressTest
接口?为了让surefire“找到”您的@Category
类,它必须位于从Maven项目的依赖关系树生成的类路径上
这个例外
无法加载类别:com.mycompany.project.common.utils.StressTest
。。。强烈暗示任何包含com.mycompany.project.common.utils.StressTest
的工件都不是模块a
的声明依赖项
因此,您需要将对包含com.mycompany.project.common.utils.StressTest
的任何工件的依赖项添加到您的moduleA
。如果此依赖项的唯一目的是提供@Category
类,则将此依赖项测试的范围限定为有意义的,例如
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>common.utils</artifactId>
<version>...</version>
<scope>test</scope>
</dependency>
然后,您可以使用模块a
中的依赖项中的测试jar
来依赖它,例如:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>common.utils</artifactId>
<version>...</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
com.mycompany
公用事业
...
试验罐
测试
如果我在test/java下定义了StressTest,那么它就不起作用了,但是如果我在main/java中定义了它,那么它就起作用了。@bluetech我已经更新了答案,以描述如何从您的通用utils模块导出并依赖一个测试jar
。
<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>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>common.utils</artifactId>
<version>...</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>