Java Code Coverage

Java Code Coverage

JaCoCo (eclemma.org/jacoco) and Cobertura (cobertura.github.io/cobertura) are free Java code coverage libraries for Java. The aim of these projects is to provide standard technologies for code coverage analysis in Java VM based environments. Their focus is on providing lightweight, flexible, and well documented libraries for integration with various build and development tools.

Features in Java code coverage libraries include code analysis of instructions, branches, lines, methods, types, and cyclomatic complexity. In the case of JaCoCo, analysis is based on Java byte code and therefore works without source files. Integration is simple and painless because everything is done via a Java agent based on on-the-fly instrumentation. Moreover, these solutions are framework agnostic in that they smoothly integrate with Java VM-based applications, such as plain Java applications, OSGi frameworks, web containers, and EJB servers. Various reporting formats are supported, including HTML, XML, and CSV.

In the IDE, integration with JaCoCo and Cobertura is included out-of-the-box for Maven applications. Setting up Java code coverage in a Maven application is as simple as adding the Maven plugin for JaCoCo to the POM:

<build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.5.201505241946 </version> <executions>

<execution> <goals>

<goal>prepare-agent</goal> </goals> </execution> <execution>

<id>report</id> <phase>prepare-package</phase> <goals>

<goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

In the case of Cobertura, the Maven Cobertura plugin is registered as follows in the POM: <reporting>

<plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.7</version>

</plugin> </plugins> </reporting>

When one of the Java code coverage plugins (and not more than one!) has been registered in the POM of a project, the IDE immediately detects that the plugin has been added. Immediately, new menu items are made available for checking the code coverage of your Maven-based project. These menu items are available when you right-click a project to which the Java code coverage Maven plugin has been added to the POM, as shown in Figure 7-8 .

Figure 7-8. Code Coverage menu items

Select the Show Report menu item to open the Code Coverage Report window, as shown in Figure 7-9 .

Figure 7-9. Code Coverage Report window

When you click Run All Tests, the tests in the application run, the Java code coverage library analyzes code coverage and collects the results, and the IDE displays the results in the Code Coverage Report window, as shown in Figure 7-9 .

Next, in the Files window (Ctrl+2), expand the project’s target folder. In the site subfolder you will see that a jacoco or cobertura folder has been created. It contains the generated reports and other output, as shown in Figure 7-10 .

Figure 7-10. JaCoCo reports and output in the Files window Each Java file, when opened in the editor, shows the statements that are being tested (green) and are not

(red), as shown in Figure 7-11 . The combination of Java code coverage libraries and the IDE helps to support test driven development because the green/red markings in the editor are continually updated, while you’re writing new tests for the statements defined in the class.

Figure 7-11. Code coverage shown in Java source file To use comparable features for Ant-based projects, go to Tools ➤ Plugins and install the TikiOne

JaCoCoverage plugin, as shown in Figure 7-12 .

Figure 7-12. Code coverage for Ant-based Java projects