Recommended sites

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits
maven2 Unit Test code reuse and dependencies Print E-mail
User Rating: / 0
PoorBest 
Thursday, 10 April 2008 14:52
maven-logo-2.gif

In a multi modules project where you have write API or common code for unit tests in one project and want to reuse these in the tests for another project. Maven will crash during the compile phase if you do not make the following.

 

Maven rules of the game:

  • The main code in src/main/java is visible across modules if you did specify project
    dependencies in pom.xml.
  • Test code reside in src/test/java and is not shared across modules, moreover
  • Test code can use any code from src/main/java but not the other way around, which
    make sense as we want to clearly separate test code (junit testcases) from code shipped.

The solution is to create additional test jar for each module, this is done by putting in the
parent pom (lets call it parent-pom.xml)

inside the <build></build> tags the following:

   1:  <plugin>
   2:    <groupId>org.apache.maven.plugins</groupId>
   3:    <artifactId>maven-jar-plugin</artifactId>
   4:    <executions>
   5:      <execution>
   6:        <goals>
   7:          <goal>test-jar</goal>
   8:        </goals>
   9:      </execution>
  10:    </executions>
  11:  </plugin>

This will create for each modules an additional jar named {artifactId}-{version}-tests.jar
during the goal test-jar

Now for every modules where you want to reuse test classes, all you have to do in to put in every
modules pom.xml a dependency to that test jar by adding the tests classifier

   1:  <dependency>
   2:        <groupId>yourGroup</groupId>
   3:        <artifactId>yourReusableModuleArtifact</artifactId>
   4:        <version>0.1-SNAPSHOT</version>
   5:        <classifier>tests</classifier>
   6:        <scope>test</scope>
   7:  </dependency>

This has work for me with Maven 2.0.8

Comments
Add New Search RSS
Write comment
Name:
Email:
 
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
:):grin;)8):p:roll:eek:upset:zzz:sigh:?:cry
:(:x
Please input the anti-spam code that you can read in the image.

3.20 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

 


Another articles:


Content View Hits : 2426274

Enter Amount: