问题描述
我想定义项目范围的接口,以在@Category
批注中使用,并配置Maven以在构建整个项目时排除其批注的测试.
I'd like to define project-wide interfaces, to be used in @Category
annotations, and configure Maven to exclude their annotated tests when building the whole project.
在 application 项目中,有一个我要分类的测试:
In the application project there's a test I'd like to categorize:
@Category(Integration.class)
@Test
public void testExternalResource() { ... }
设置:
我已经建立了一个多模块的Maven项目:
The setting:
I've set up a multi-module maven project:
/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 模块中,我定义了:
Being an infrastructure freak :), I'd like to configure my whole project to exclude groups in any module. So, in parent module I've defined:
<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
中)中,所有模块都可以看到:
And I've put the Integration
interface in the util module (in the util/src/test/java
), for all modules to see:
package com.mycompany.test;
public interface Integration {}
并且因为它是test-jar
,所以我已进行了正确的配置对于 util 和 application .
And because it's a test-jar
, I've made the right configuration for both util and application .
运行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
当然,该类不可用-我在项目的第一个模块的测试罐中塞满了它. :(
Well, of course the class isn't available - I've crammed it in a test-jar in the first module of the project. :(
我应该在哪里放置我的Integration
界面,以便其对我的mvn配置和我的源代码可用?
Where should I put my Integration
interface so it will be available to my mvn configuration and my source code?
推荐答案
创建一个新的Maven模块并将该接口放在src/main/java/
下.确保junit
可用作编译时依赖项(<scope>compile</scope>
)-您基本上是在构建其他测试可以使用的依赖项.因此,像往常一样,在其他测试进入src/test/java
时,必须将有助于其他测试的代码放入main中.
Create a new Maven module and put the interface under src/main/java/
. Make sure junit
is available as a compile time dependency (<scope>compile</scope>
) - you're basically building a dependency that other tests can use. So your code that helps other tests must go into main while the tests for this go in the src/test/java
, as usual.
这一步感觉有点奇怪,但是请考虑如何打包junit
本身.从junit
的角度来看,它只是一个普通的Java库,因此其所有代码都放入了main
.
This step feels a bit weird but think about how you would package junit
itself. From the point of view of junit
, it's just a normal Java library, so all its code goes into main
.
当您需要此模块时,可将其用作<scope>test</scope>
When you need this module, use it as a test dependency with <scope>test</scope>
这篇关于我应该将Junit @Category的接口类放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!