190

How can you depend on test code from another module in Maven?

Example, I have 2 modules:

  • Base
  • Main

I would like a test case in Main to extend a base test class in Base. Is this possible?

Update: Found an acceptable answer, which involves creating a test jar.

Community
  • 1
  • 1
flicken
  • 14,624
  • 4
  • 27
  • 27
  • 1
    It seems an answer equivalent to the combination of the accepted answer below, which is actually a note on the larger answer further below, is now at the Maven site: [How to create a jar containing test classes](https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html) – David Tonhofer Aug 17 '17 at 10:09

4 Answers4

196

I recommend using type instead of classifier (see also: classifier). It tells Maven a bit more explicitly what you are doing (and I've found that m2eclipse and q4e both like it better).

<dependency>
  <groupId>com.myco.app</groupId>
  <artifactId>foo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <type>test-jar</type>
  <scope>test</scope>
</dependency>
David Tonhofer
  • 12,954
  • 4
  • 44
  • 46
Ben
  • 4,303
  • 3
  • 23
  • 37
  • 5
    Should there be a separate dependency entry for com.myco.app specially for the test-jar type? – Noah Watkins Apr 04 '12 at 00:50
  • 20
    Also remember to add test-jar to maven-jar-plugin configuration: https://maven.apache.org/guides/mini/guide-attached-tests.html – user1338062 Nov 28 '12 at 11:31
  • 12
    I was confused when first reading this answer... That's actually because it does not make sense on its own, you should first read the below answer: http://stackoverflow.com/questions/174560/sharing-test-code-in-maven#174670 – TanguyP Jul 28 '15 at 15:24
190

Thanks for the base module suggestion. However, I'd rather not create a new module for just this purpose.

Found an acceptable answer in the Surefire Maven documentation and a blog. See also "How to create a jar containing test classes".

This creates jar file of code from src/test/java using the jar plugin so that modules with tests can share code.

<project>
  <build>
    <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-jar-plugin</artifactId>
       <version>2.4</version>
       <executions>
         <execution>
           <goals>
             <goal>test-jar</goal>
           </goals>
         </execution>
       </executions>
     </plugin>
    </plugins>
  </build>
</project>

In order to use the attached test JAR that was created above you simply specify a dependency on the main artifact with a specified classifier of tests:

<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> 
David Tonhofer
  • 12,954
  • 4
  • 44
  • 46
flicken
  • 14,624
  • 4
  • 27
  • 27
  • 14
    Note, that there can be issues with using teststest-jar. Here's one issue in Maven http://jira.codehaus.org/browse/MNG-2045 and an unrelated one in IntelliJ http://youtrack.jetbrains.net/issue/IDEA-54254 – Emil Sit May 04 '10 at 19:10
  • It has been very useful to me, but I have found a problem: When I execute "install -Dmaven.test.skip=true", also the dependency test-jar is required and the proccess fails – Javi Pedrera Jul 03 '13 at 16:47
  • @JaviPedrera it works for me even if I do 'mvn clean install -DskipTests=true' and the test-jar will be creating. no errors – Karussell Aug 26 '13 at 14:13
  • @Karussell, maybe you have created the jar in the maven repository before, have you tried to remove it and execute the command? – Javi Pedrera Aug 26 '13 at 18:33
  • @JaviPedrera hmmh still works for me. probably you have an oldish maven? – Karussell Aug 27 '13 at 13:34
  • @jontejj There was a problem: I found resource file such as spring context in test-jar could not be load in class path ,but classes in test-jar can be load well.@Emil Sit I use IntelliJ 14.1, classifier and type tag in dependency I tried but these didn't work either. – Allen Jun 05 '15 at 02:50
  • 1
    @Allen have you made sure that you use the ServiceResultTransformer while packaging your jar? Otherwise you may end up with service files overwriting each other. – jontejj Jun 05 '15 at 06:44
  • @jontejj I'm newer to Java, What is "ServiceResultTransformer" you mentioned before? My config files in test resource dir do not have same file name or bean definition, overwritten perhaps won't happen. – Allen Jun 06 '15 at 04:10
  • 1
    https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html there you can read more about it. – jontejj Jun 06 '15 at 13:08
  • @jontejj Thanks for your help.But I don't want to Aggregating classes/resources from several artifacts into one uber JAR, All I wanted is to load spring context config in another test jar in maven test phase. And when I append shade plugin, problem still happened. – Allen Jun 08 '15 at 08:08
13

We solved this by making a maven project with test code as the src/main/java and adding the following dependency to projects:

    <dependency>
        <groupId>foo</groupId>
        <artifactId>test-base</artifactId>
        <version>1</version>
        <scope>test</scope>
    </dependency>
sal
  • 22,528
  • 15
  • 64
  • 84
  • Yep, that'd work, thanks! See my comment below for alternate answer that I prefer. – flicken Oct 06 '08 at 15:12
  • 1
    We use this approach as well, it's a bit silly you are forced to go for classifiers or types (that are hardly basics in Maven for most of the users) and that you have to build JAR with some effort when you don't really need it or - as in this case - you have de-facto non-test code as a base only for the test code. – virgo47 Nov 15 '12 at 11:37
-3

Yep ... just include the Base module as a dependency in Main. If you're only inheriting test code, then you can use the scope tag to make sure Maven doesn't include the code in your artifact when deployed. Something like this should work:

<dependency>
    <groupId>BaseGroup</groupId>
    <artifactId>Base</artifactId>
    <version>0.1.0-SNAPSHOT</version>
    <scope>test</scope>
</dependency>
Steve Moyer
  • 5,487
  • 20
  • 31