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

JUnit categories with multi module project - Unable to load category

我想include/exclude JUnit 分类测试。我在公共模块中定义了一个标记接口 StressTest。我在模块 A 中引用 StressTest。我在根 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 test 我在构建另一个模块时得到以下信息。

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

我应该在哪里写我的 StressTest 接口?

为了确保 'find' 您的 @Category class 它必须位于从您的 Maven 项目的依赖树生成的 class 路径上。

这个例外...

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

... 强烈暗示包含 com.mycompany.project.common.utils.StressTest 的任何工件 不是 您的 moduleA.

的声明依赖项

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

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

此外,如果 com.mycompany.project.common.utils.StressTesttest 树中,而不是在你的通用 utils 模块中的 main 树中,那么你将需要创建一个包含通用实用程序模块测试 classes 的 JAR。您可以通过将以下 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>

然后您将在 moduleA 的依赖项中使用 <type>test-jar</type> 来依赖它,例如:

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