Reputation: 21
I have a set of functional jars(more than 3) that tests my source code. These jars just contains test classes and assisting asserter classes. I am creating a new performance jar that would import all the functional tests from these jars so that all can be run simultaneously. But when I include them as test dependencies in pom of current jar, what all I get to see is the classes in src/main/java. How can I include these functional jars as dependent jars so that I can also reference classes in src/test/java. In other words, how do I reference the test classes in other jars. In what way should I include the dependency as.
Thanks for your support.
Upvotes: 2
Views: 738
Reputation: 570345
Put your common "test" code in a separate module (in src/main/java
) and declare a dependency on this module with a test
scope in modules that need the common code:
<dependency>
<groupId>my.group.id</groupId>
<artifactId>testing-framework</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
That's a common practice, there is nothing wrong with that.
Use the Maven JAR Plugin and its jar:test-jar
goal to build a JAR of the test classes for the current project as part of the package
phase (jar:test-jar
binds itself by default on package
):
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
To use the attached test JAR that was created above, the recommended way is to specify a dependency on the main artifact with a specified type of test-jar
:
<project>
...
<dependencies>
<dependency>
<groupId>com.myco.app</groupId>
<artifactId>foo</artifactId>
<version>1.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>
See the discussion at thee bottom of Guide to using attached tests for why <type>test-jar</type>
should be preferred over <classifier>tests</classifier>
.
Upvotes: 4
Reputation: 298898
Either go the way suggested by Lauri or use a slight variation where you don't have a dedicated test artifact but a commons artifact that also has common test classes in src/test/java.
In this project, use the test-jar mojo to create and attach the test jar of this project
Now in derived projects, you can include the dependency like this:
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<scope>test</scope>
<classifier>test</classifier>
</dependency>
that way you don't need to create a separate test project if you already have a commons project
Upvotes: 2
Reputation: 3424
mvn package
doesn't jar up test classes and resources. So you'd have to put the dependencies into src/main/java.
Whenever I do this I create a jar project called "someproj-testutils" and have helper classes or base test classes and resources in the normal src/main/
locations and then reference someproj-testutils as a test scoped dep.
Upvotes: 1
Reputation: 10857
The code in src/test/java
is not included in the jar by default (I consider this to be a good thing and would be cautious about changing it even if I could).
I find putting common test code into a separate maven module's src/main/java
to be a very good practice. Then, just include this module as a test-scoped dependency where you need it.
Upvotes: 3