tags:

views:

96

answers:

5

Hi,

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.

+3  A: 

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.

Lauri Lehtinen
+1  A: 

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.

scotth
I don't see a reason to downvote this. will somebody elaborate?
seanizer
I didn't downvote but you do NOT *have to put the dependencies into src/main/java*, this is just one option.
Pascal Thivent
true. it is the standard option used in 95% of cases, though.
seanizer
+2  A: 

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

seanizer
+2  A: 

First option

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.

Second option

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

Pascal Thivent
The second option suits the best for my user scenario. I generally use the same way as all other people mentioned, but what I am trying to do is not to use the asserters or some other code of the test jar. What all I am trying to achieve is to run the tests in "ABC" jar from "XYZ" jar by just referencing them from the XYZ jar.
I am able to build the "ABC" jar as the test-jar and use it as a dependency in "XYZ" jar. But will that dependency pull all the transitive dependencies that "ABC" jar needs to run? I see that adding it as a test-jar just pulls the test-classes in "ABC" jar but not other dependencies that are needed to run those tests.Thanks for all your valuable suggestions.
A: 

The second option suits the best for my user scenario. I generally use the same way as all other people mentioned, but what I am trying to do is not to use the asserters or some other code of the test jar. What all I am trying to achieve is to run the tests in "ABC" jar from "XYZ" jar by just referencing them from the XYZ jar.

I am able to build the "ABC" jar as the test-jar and use it as a dependency in "XYZ" jar. But will that dependency pull all the transitive dependencies that "ABC" jar needs to run? I see that adding it as a test-jar just pulls the test-classes in "ABC" jar but not other dependencies that are needed to run those tests. Thanks for all your valuable suggestions.