Separate unit and integration testcases using JUnit Category.

This post is about separating Unit and Integration testcases using JUnit category and running those separately using maven. This post in NOT about moving unit and integration testcases into separate test source folders. With the introduction of org.junit.experimental.categories.Category annotation in JUnit 4.x we are in a position to separate integration testcases using annotation. Basically Category annotation gives the programmer capability to categorize the testcases.

Tools libraries used.

  1. JDK 1.5 or later
  2. JUnit 4.8.1
  3. maven-surefire-plugin 2.16
  4. maven-failsafe-plugin 2.16

Where to find the complete source code?

Complete source code can be cloned from git.

Steps to separate integration testcases.

  1. Write an interface for Integration testcase category.
  2. Use Category annotation to mark integration testcases.
  3. Add maven-surefire-plugin to test unit testcases.
  4. Add maven-failsafe-plugin to test integration testcases.
Write an interface for Integration testcase category.

This interface shall be passed as an argument to Category annotation. This means that you can separate your testcases like service layer testcases, web layer testcases, slow testcases etc.

Use Category annotation to mark integration testcases.
Add maven-surefire-plugin to test unit testcases.

Below given line is important.

Its directing the maven-surefire-plugin plugin to skip group com.ourownjava.tdd.IntegrationTest.

Add maven-failsafe-plugin to test integration testcases.

How to run unit testcases alone?

“mvn test” would run only the unit testcases.

How to run the unit testcases and integration testcases?

“mvn verify” would run both unit and integration testcases.

 

 

 

 

 

One thought on “Separate unit and integration testcases using JUnit Category.

Leave a comment

Your email address will not be published. Required fields are marked *