我应该在哪里为Junit @Category设置接口类?

Asa*_*saf 8 junit integration-testing maven maven-surefire-plugin

我想定义项目范围的接口,用于@Category注释,并配置Maven在构建整个项目时排除它们的注释测试.

应用程序项目中,有一个我要分类的测试:

@Category(Integration.class)
@Test
public void testExternalResource() { ... }
Run Code Online (Sandbox Code Playgroud)

那个设定:

我已经建立了一个多模块maven项目:

/container (has a pom with <modules> element)
    /parent (all other modules inherit from its pom. has no source, only pom)
    /util (other modules are depending on it)
    /infra
    /application (depending on infra and util, inherits from parent)
Run Code Online (Sandbox Code Playgroud)

作为一个基础设施狂热者:),我想配置我的整个项目以排除任何模块中的组.所以,在模块中我定义了:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
         <excludedGroups>com.mycompany.test.Integration</excludedGroups>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

我将Integration接口放在util模块中(在util/src/test/java)中,所有模块都可以看到:

package com.mycompany.test;
public interface Integration {}
Run Code Online (Sandbox Code Playgroud)

因为它是一个test-jar,我已经utilapplication 做了正确的配置.

错误:

跑步的时候mvn clean install,我得到了

[ERROR] Failed to execute goal org.apache.maven.plugins:
    maven-surefire-plugin:2.16:test
    (default-test) on project util: Execution default-test of goal
    org.apache.maven.plugins:maven-surefire-plugin:2.16:test
    failed: There was an error in the forked process
[ERROR] java.lang.RuntimeException:
    Unable to load category: com.mycompany.test.Integration
Run Code Online (Sandbox Code Playgroud)

嗯,当然这个课程不可用 - 我已经在项目的第一个模块中的测试罐中塞满了它.:(

我应该在哪里放置我的Integration界面,以便它可用于我的mvn配置和我的源代码?

Aar*_*lla 11

创建一个新的Maven模块并将接口放在下面src/main/java/.确保junit可用作编译时依赖项(<scope>compile</scope>) - 您基本上构建了其他测试可以使用的依赖项.所以你的代码可以帮助其他测试必须进入main,而对此的测试src/test/java就像往常一样.

这一步感觉有点奇怪,但想想你将如何打包junit自己.从它的角度来看junit,它只是一个普通的Java库,因此它的所有代码都会进入main.

当您需要此模块时,请将其用作测试依赖项 <scope>test</scope>