How I tried to fix certain programming problems, mostly in the java, JEE, JBoss scene, web area and using Ubuntu or Debian linux.

Sonar integration test coverage

I am working on a large multi-module project and we are using sonar (now named SonarQube) for quality monitoring. SonarQube offers a lot of interesting information without extra configuration (especially if you are already using maven).

In this project, we aim to make the build fail fast, so we have three levels of testing. In each module we have a set of unit tests (white box testing which tests the individual class with mocked out interactions). Each module also has integration tests to verify that the individual pieces fit together. These integration tests use the database, spring context, drools rules and activiti flows but still test directly (direct java calls on the spring services). At a higher level, we have integration tests which verify that our REST interface works properly and that the various modules work together properly.

Unfortunately though, our sonar instance only counted the unit test coverage and not the integration test coverage. The aim is to get information like this:

Let’s first see some snippets from the original configuration. The running of the unit tests is done using surefire (test classes named *Test) and integration tests are done using the failesafe plugin (test classes named *IT).

To make sure that sonar can report integration test coverage, we need to make some changes. For starters, we need to define some properties for the sonar plugin. These need to be added in the properties section of your pom.

Here we tell sonar that this is a Java project and that jacoco should be used to determine the coverage. We also tell sonar to reuse the existing reports in the build. This speeds up your build in Jenkins. Using reuseReports prevents the “mvn sonar:sonar” from re-running the tests and just reading the results from the previous run.
The last two settings define the locations where sonar can find the coverage information which is built using jacoco.

To allow jacoco to gather the coverage information, you have to install an agent when starting the java process. A practical way to determine the commandline parameter to use this agent, you can use the jacoco maven plugin.

This plugin can be used to set an “argLine” property which can be used when starting a JVM. As the jacoco instrumentation increases the build time a little, I put this in a profile to assure it is not automatically included. In Jenkins, “-Dcoverage” is added in the maven “Goals and options” part of the “Build” configuration.

The jacoco plugin builds the “argLine” property in the prepare-agen goal and produces reports in the target/site directory in the report goal. The includes property indicates the classes for which coverage should be reported. This needs to be changed to match your project root package.

To assure that the argLine property is also defined when no coverage is needed, I also had to add the nocoverage profile. This is automatically enabled when you do not specify -Dcoverage on your maven command line.

The test runners now need to be changed to include the jacoco agent as stored in the argLine property.

The only change is the configuration of the argLine. This specifies the arguments to pass to the JVM when forking to run the tests. You can also add heap configurations if needed.

Now the module specific integration tests also have their coverage reported and this is included under the unit test coverage in sonar. The integration test coverage will now also be reported (if you have that widget enabled on your dashboard), but it will always report 0.0% coverage.

The 0.0% in sonar is there because the sonar.jacoco.itReportPath property is set. However, we have not used the property to store coverage data.

In the module which does the integration tests using REST calls, the coverage data needs to be put in the itReportPath. I have redefined the coverage profile to do this. In this profile I also redefined the failsafe plugin not to include the argLine parameter. The coverage data needs to be captured in the jetty instance which runs the was, not in the test code.

Last but not least, we need to change the jetty configuration to capture the coverage data. Unfortunately, this seems impossible using the jetty maven plugin. This plugin normally uses the JVM in which maven is running. You can use a forked JVM using the run-forked goal, but I did not find a way to wait for the deploy to finish initializing.

You can fix this by using the cargo plugin to start jetty instead (or another servlet engine or application server).

It is important to set the correct context and pingURL to allow cargo to know then you war has deployed. As the REST tests are run inside a war module, we have no need to explicitly set the artifact to deploy.

This is it. The configuration as shown above combines all module specific test in the “unit test coverage” section in sonar and puts the REST tests under the “integration test coverage” heading.

If you prefer to have only real unit tests under the “unit test coverage” section and all integration tests together, then you change the configuration of the jacaco plugin.

The package phase is just before pre-integration-test, so this assures that the argLine property is redefined between the test and integration-test phases, making sure that the failsafe plugin uses the itReportPath argLine.

Great post, it did get me started on the configuration
I found it a bit confusing though since it’s a completely different approach: our integration tests are written next to our unit tests and we don’t require a Jetty to do the integration tests (we use Spring).