【问题标题】:Where should I put interface class for Junit @Category?我应该在哪里放置 Junit @Category 的接口类?
【发布时间】:2013-11-27 16:19:00
【问题描述】:

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

application 项目中有一个我想分类的测试:

@Category(Integration.class)
@Test
public void testExternalResource() { ... }

设置:

我已经建立了一个多模块的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)

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

<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>

我已经将Integration 接口放在了util 模块中(在util/src/test/java 中),供所有模块查看:

package com.mycompany.test;
public interface Integration {}

因为它对于 utilapplication 都是 test-jar, I've made the right configuration

错误:

在运行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

嗯,当然这个类是不可用的——我已经把它塞进了项目第一个模块的一个测试罐子里。 :(

我应该将我的Integration 接口放在哪里,以便我的 mvn 配置和源代码可以使用它?

【问题讨论】:

    标签: maven junit integration-testing maven-surefire-plugin


    【解决方案1】:

    新建一个Maven模块,将接口放在src/main/java/下。确保 junit 可用作编译时依赖项 (&lt;scope&gt;compile&lt;/scope&gt;) - 您基本上是在构建其他测试可以使用的依赖项。因此,帮助其他测试的代码必须像往常一样进入 main,而针对此的测试进入 src/test/java

    这一步感觉有点奇怪,但想想你将如何打包junit 本身。从junit的角度来看,它只是一个普通的Java库,所以它的所有代码都进入了main

    当你需要这个模块时,用&lt;scope&gt;test&lt;/scope&gt;作为测试依赖

    【讨论】:

    • 这当然要求这个新模块不会从使用它的父 pom 继承?我试试看
    • 您可以与父 POM 建立循环依赖关系。为此,父 POM 必须具有 &lt;packaging&gt;pom&lt;/packaging&gt;,这意味着它实际上不会尝试加载您在此处定义的依赖项。如果父 POM 实际上是主代码的 POM,则创建第二个 Maven 项目。
    • 父级:无源,pom打包
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2012-08-25
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多