具有多模块项目的JUnit类别 - 无法加载类别

时间:2017-10-10 21:01:19

标签: java maven unit-testing testing junit4

我想要包含/排除JUnit分类测试。我在公共模块中定义了标记接口StressTest。我在moduleA中引用了StressTest。我在root pom.xml

中有以下maven-surefire-plugin
<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>

运行mvn测试时,我在构建另一个模块时会得到以下内容。

Unable to load category: com.mycompany.project.common.utils.StressTest

我应该在哪里写StressTest界面?

1 个答案:

答案 0 :(得分:2)

为了确保找到&#39;你的@Category类必须在你的Maven项目的依赖树产生的类路径上。

此例外......

  

无法加载类别:com.mycompany.project.common.utils.StressTest

...强烈暗示包含com.mycompany.project.common.utils.StressTest的任何工件moduleA声明的依赖关系。

因此,您需要向com.mycompany.project.common.utils.StressTest添加对包含moduleA的任何工件的依赖关系。如果此依赖项的唯一目的是提供@Category类,那么将此依赖项测试作为范围是有意义的。

<dependency>
    <groupId>com.mycompany</groupId>
    <artifactId>common.utils</artifactId>
    <version>...</version>
    <scope>test</scope>
</dependency>

此外,如果com.mycompany.project.common.utils.StressTest位于测试树中,而不是公共utils模块中的 main 树,那么您需要创建一个包含common utils模块的测试类。您可以通过将以下maven-jar-plugin声明添加到常用工具来实现此目的。 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>