





















































This article by Achim Nierbeck, one of the authors of Apache Karaf Cookbook, describes how we can find the coverage of a test.
(For more resources related to this topic, see here.)
Apart from testing the application, it is usually also a requirement to know how well the unit and integration tests actually cover the code. For code coverage, a couple of technologies are available. This recipe will cover how to set up your test environment to find the coverage of the test.
The sources of this recipe are available at https://github.com/jgoodyear/ApacheKarafCookbook/tree/master/chapter10/chapter10-recipe4.
To find out about the coverage of the test, a code coverage tool is needed. We will take the Java Code Coverage Library as it has a Maven plugin for automated coverage analysis. At first, the Maven coordinates for the plugin are added as shown in the following code:
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.0.201403182114</version>
We need to prepare the code first so it can be covered by the agent as follows:
<execution>
<id>prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<propertyName>jcoverage.command</propertyName>
<includes>
<include>com.packt.*</include>
</includes>
<append>true</append>
</configuration>
</execution>
This will include the com.packt package, including subpackages. After the integration tests are done, the test report needs to be generated as follows:
<execution>
<id>report</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
Besides these additions to the POM configuration, you need to add the VM options to the configuration of the Apache Karaf test. Without setting these options to the virtual machine, which executes the test, the executing environment doesn't know of the coverage and, therefore, no coverage is done. This can be done as follows:
private static Option addCodeCoverageOption() { String coverageCommand = System.getProperty(COVERAGE_COMMAND); if (coverageCommand != null) { return CoreOptions.vmOption(coverageCommand); } return null; }
The resulting report of this coverage looks like the following screenshot. It shows the coverage of the CalculatorImpl class and its methods. While the add method has been called by the test, the submethod wasn't. This results in zero coverage for that method.
First, you need to prepare the agent for covering, this will be inserted into the jcoverage.command property. This property is passed to the test by adding the vmOption directory. This way the coverage agent is added to the Java Virtual Machine and it tracks the coverage of the test execution. After the test is run successfully, the report is generated by the jacoco-maven-plugin. All of this works fine with a single Maven module. A multimodule project setup will require additional work, especially if you want to combine unit and integration test coverage. More details can be found at http://www.eclemma.org/jacoco/index.html.
This recipe describes how we can set up a test environment to find the coverage of a test.
Further resources on this subject: