tags:

views:

175

answers:

2

I've got a test class in a module that extends another test class in one of its dependency modules. How can I import the dependency's test code into the test scope of the dependent module?

To illiterate, I've got two modules, "module-one" being a dependency of "module-two". SubTestCase is a subclass of TestCase.

module-one
          \src\test\java\com\example\TestCase.java
module-two
          \src\test\java\com\example\SubTestCase.java

But the build is failing because the test code of "module-one" is not being imported into "module-two", just the main code.

A: 

Usually this is solved by building and deploying modulename-test.jar files in addition to the regular modulename.jar file. You deploy these to repos like regular artifacts. This is not totally flawless, but works decently for code artifacts.

Then you would add test scoped dependencies to the test-jars to other modules.

You can also solve this by putting test scoped artifacts in "main" scope in a separate module of its own and then include this in regular test-scope in other modules. This solution does not work very well in a multi-module build where each module exports some test artifacts, since you basically get 2N modules.

A lot of us actually give up on both of these solution when we realize that the number of classes is fairly limited and there are problems associated with both of these solution. We just put them in an appropriately named package in "main" scope. I just keep forgetting why the two first solutions are a pain.

krosenvold
+2  A: 

You can deploy the test code as an additional artifact by using the maven-jar-plugin's test-jar goal. It will be attached to the project and deployed with the classifier tests.

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>test-jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Other projects can then reference the test jar by declaring the tests classifier in the dependency.

<dependency>
  <groupId>name.seller.rich</groupId>
  <artifactId>foo</artifactId>
  <version>1.0.0</version>
  <classifier>tests</classifier>
  <scope>test</scope>
</dependency>
Rich Seller