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.
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>
                        Regarding Rich Seller's answer:
The use of <classifier>tests</classifier> is out dated see the user’s guide.
I am using maven 2.2.1 and maven-jar-plugin 2.2 and it required to 
switch <type>test-jar</type> instead of <classifier>tests</classifier>.
Note that tests jar are not transitive and so you may need to add them explicitly.
<project>
    ...
    <dependencies>
        <dependency>
            <groupId>name.seller.rich</groupId>
            <artifactId>foo</artifactId>
            <version>1.0.0</version>
            <type>test-jar</type>
            <scope>test</scope>
         </dependency>
    </dependencies>
     ...
</project>
Update following Mike Sokolov comment:
The user’s guild for maven  3 updated on 2014-03-28 see link above say’s 
Note that previous editions of this guide suggested to use <classifier>tests</classifier> instead of <type>test-jar</type>. While this currently works for some cases, it does not properly work during a reactor build of the test JAR module and any consumer if a lifecycle phase prior to install is invoked. In such a scenario, Maven will not resolve the test JAR from the output of the reactor build but from the local/remote repository. Apparently, the JAR from the repositories could be outdated or completely missing, causing a build failure (cf. MNG-2045).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With