John Dobie is an independent consultant specialising in Java enterprise architecture and development. He is a certified as a Java architect, SCWCD, programmer and ScrumMaster(CSM) with over 15 years of Java experience. Most of the last 15 years has been spent in London in Finance IT working on Java trading and banking systems. Currently he specialises in leading and mentoring teams to improving their Agile engineering practices. John is a DZone MVB and is not an employee of DZone and has posted 11 posts at DZone. You can read more from them at their website. View Full User Profile

Easy Unit and Integration Code Coverage

  • submit to reddit

This example shows how to generate coverage for unit and integration tests using Maven and Sonar.
It uses very simple techniques and should only take 10-15 minutes to get running in any existing Maven build.
It can be used across unit, integration, ATDD or any other kind of test suite.
The coverage results are shown in Sonar.

Whats Coming?

My previous post showed how we can use JUnit categories to easily split unit and integration test suites.

The next logical step is to be able to look at metrics for each test suite.
This example shows how to do that using Jacoco and Sonar.


The code for the example is here.
svn co
mvn clean install sonar:sonar


This example relies on Sonar to show the code coverage metrics. Sonar is a fanatastic open source code quality tool that everyone should have a look at.

For our example there are a couple of simple configuration changes that are needed.
The following link shows how to install Sonar and make the changes

Splitting The Test Suites.

This example relies on JUnit categories to split our tests.
We define a marker interface and then apply it to the tests we want to split.
public interface IntegrationTest {}
The category annotation is added to your test class. It takes the name of your new interface.
import org.junit.experimental.categories.Category;
public class ExampleIntegrationTest{
 public void longRunningServiceTest() throws Exception {
The whole process is very simple and is fully explained here

Analyzing The Code Coverage

We use the jacoco plugin to do the code coverage. There is an overview of Jacoco here.
We first define the directories for the jacoco coverage files.

Configure the Unit Tests

Then we start the unit tests by running the standard the surefire plugin with the Jacoco agent pointing to ${sonar.jacoco.reportPath}.  This is used to store the unit test code coverage results.
we ignore any marked integration tests with the following config

Configure the integration tests

For the Integration tests we use the failsafe plugin and point the Jacoco agent to ${sonar.jacoco.itReportPath}.  This is used to store the integration test code coverage results.
We also tell the plugin to use the correct JUnit categories

When these are run they will produce the following 2 coverage files.

Start Sonar.

Before running the build you need to start your Sonar server.

Running The Example

We can run the whole lot using the following command
mvn clean install sonar:sonar
You will see the following results if you browse to your sonar instance.



What's Next? 

The tests in this example leave a lot to be desired in terms of realism.

Now the technique has been covered I will do a more realistic example and post this soon. 


Published at DZone with permission of John Dobie, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)