Wednesday, January 4, 2017

How to reuse java tests in other projects

It often happens that tests or classes, defined under the test folder, are relevant for another projects.
It is possible to share the test classes by creating a test-jar for the "home" project. The other project can use the tests by adding the test-jar as a dependency.
The post explains, how to add test-dependency in maven and sbt projects.

Creation of test jar


In pom.xml of the maven project define maven-jar-plugin as following:
 <project  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"  
      xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
      <modelVersion>4.0.0</modelVersion>  
      <groupId>com.myproject.commons</groupId>  
      <artifactId>logging</artifactId>  
      <version>1.2.3-SNAPSHOT</version>

      <dependencies>  
       ...  
      </dependencies>

      <build>  
         <plugins>  
          <plugin>  
             <groupId>org.apache.maven.plugins</groupId>  
             <artifactId>maven-jar-plugin</artifactId>  
             <version>2.6</version>  
             <executions>  
              <execution>  
               <goals>  
                <goal>test-jar</goal>  
               </goals>  
              </execution>  
             </executions>  
            </plugin>            
         </plugins>  
      </build>  
    ...  
When the project is built with either mvn install or mvn deploy commands, two jars are created:
- logging-1.2.3-SNAPSHOT.jar - a regular jar;
- logging-1.2.3-SNAPSHOT-tests.jar - a tests jar.

Other project can add dependency to either of the jars or to both of them.
If the test-jar is added as a dependency, its classes may be used for test classes of the project.

Adding test-jar to dependencies


The examples below shows how to add both jars to the dependencies, but it is possible to add the test-jar dependency only.

For maven project:
 <project  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"  
      xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
      <modelVersion>4.0.0</modelVersion>  
      <groupId>com.myproject.commons</groupId>  
      <artifactId>services</artifactId>  
      <version>2.2.2-SNAPSHOT</version>  
      <properties>  
           <logging.version>1.2.3-SNAPSHOT</logging.version>  
      </properties>  
      <dependencies>  
           <!-- logging dependencies -->  
           <dependency>  
                <groupId>com.myproject.commons</groupId>  
                <artifactId>logging</artifactId>  
                <version>${logging.version}</version>  
           </dependency>  
           <!-- logging tests dependencies-->  
           <dependency>  
                <groupId>com.myproject.commons</groupId>  
                <artifactId>logging</artifactId>  
                <type>test-jar</type>  
                <version>${logging.version}</version>  
                <scope>test</scope>  
           </dependency>  
      </dependencies>  
    ...  
You can read also this page of the Maven documentation.

For sbt project:
 ...  
 val loggingVersion= "1.2.3-SNAPSHOT"  
 libraryDependencies ++= Seq(  
     "com.myproject.commons" % "logging" % loggingVersion,  
     "com.myproject.commons" % "logging" % loggingVersion% "test" classifier "tests",  
 )  
 ...  

4 comments :

for IT the said...

I have read your blog its very attractive and impressive. I like it your blog.

Do you have any step to step instructions for doing in eclipse mars please

Java Training in Chennai

Ala Schneider said...

Hi, I am glad you like the blog.

Do you mean step to step instruction for reusing tests?

Ala

pay for essay service said...

Hey this was really well explained. You have certainly put some heart into this post. It proved to helpful for me and im sure others are going to benefit from this too. :)

Ala Schneider said...

Thank you for the feedback. I am pleased my work is helpful.

About the author

My Photo
I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.
Back to Top