问题
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.
In the application project there's a test I'd like to categorize:
@Category(Integration.class)
@Test
public void testExternalResource() { ... }
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)
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>
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 {}
And because it's a test-jar
, I've made the right configuration for both util and application .
The error:
When running mvn clean install
, I'm getting
[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. :(
Where should I put my Integration
interface so it will be available to my mvn configuration and my source code?
回答1:
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.
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
.
When you need this module, use it as a test dependency with <scope>test</scope>
来源:https://stackoverflow.com/questions/19981320/where-should-i-put-interface-class-for-junit-category