JMockit An automated testing toolkit for Java

Measuring code coverage

  1. Line coverage
  2. Path coverage
  3. Data coverage
  4. Types of coverage output
    1. Call points
  5. Configuring the coverage tool
  6. Aggregated reports for multiple test runs
    1. Generating an aggregate report from multiple data files
    2. Generating an aggregate report from a single data file appended after each test run
  7. Checking minimum coverage
  8. Activating coverage in a Maven project
    1. Including the HTML report in a Maven site
  9. Turning coverage off
  10. Standalone mode

Code coverage consists of a set of software metrics that can tell you how much of the production code is covered by a given test suite. It's purely quantitative, and does not say anything about the quality of either the production code or the test code. That said, the examination of code coverage reports will sometimes lead to the discovery of unreachable code which can be eliminated. But more importantly, such reports can be used as a guide for the discovery of missing tests. This is not only useful when creating tests for existing production code, but also when writing tests first, such as in the practice of TDD (Test Driven Development).

JMockit Coverage provides three different and complementary code coverage metrics: line coverage, path coverage, and data coverage. An example coverage report showing all metrics can be found on-line.

Line coverage

The line coverage metric tells us how much of the executable code in a source file has been exercised by tests. Each executable line of code can be uncovered, covered, or partially covered. In the first case, none of the executable code in it was executed at all. In the second, all of the code was fully executed at least once. In the third case, only part of the executable code in the line was executed. This can happen, for example, with lines of code containing multiple logical conditions in a complex boolean expression. JMockit Coverage identifies all three cases, computing the coverage percentage for each executable line of code accordingly: 0% for an uncovered line, 100% for a covered line, or some value in between for a partially covered line.

A branching point exists wherever the program makes a decision between two possible execution paths to follow. Any line of code containing a logical condition will be divided in at least two executable segments, each belonging to a separate branch. An executable line of source code with no branching points contains a single segment. Lines with one or more branching points contain two or more executable segments, separated by consecutive branching points in the line.

Lets say that NS >= 1 is the number of executable segments on a given line. If NE is the number of segments in that line which were executed at least once during a test run (ie, they are covered segments), then we can calculate the coverage percentage for the line as 100 * NE / NS.

Similarly, the line coverage percentage for a whole source file is calculated from the total number of executable segments and the total number of covered segments, considering all executable lines of code in the file. The percentage for a package, in turn, is calculated from the total and covered numbers of segments in the whole set of source files belonging to the package. Finally, the total code coverage percentage is computed by the same formula on the totals for all packages.

Path coverage

A completely different metric is path coverage, which is computed for method and constructor bodies, not for lines or segments of code. It tells us how many of the possible execution paths through a method or constructor, from entry to exit, have been executed at least once during the test run.

Note that each method or constructor has a single point of entry, but can have multiple exits. An exit occurs when a return or throw statement is executed. These are normal exits, of course. A method/constructor execution can also terminate abruptly, by propagating an exception (or error) thrown as a result of a method call, an attempt to access a null reference, or some other action which caused an unintended program failure.

Each possible path can be either fully executed (covered) or not (uncovered). Paths that execute only partially (ie, they were terminated abruptly) are simply considered as uncovered.

The path coverage percentage for a method or constructor body is computed in a way similar to the line coverage computation. If NP is the number of possible paths through the implementation body and NPE is the number of paths executed from entry to exit, then the metric is computed as 100 * NPE / NP. Also in the same way as the line coverage metric, we extend this formula to the whole source file, the whole package, and the whole set of packages touched by the test run.

Data coverage

Measures how many of the instance and static non-final fields were fully exercised by the test run. To be fully exercised, a field must have the last value assigned to it read by at least one test. The percentages are calculated as 100 * NFE / NF, where NF is the number of non-final fields and NFE the number of fully exercised fields.

Types of coverage output

The JMockit Coverage tool can generate the following types of output:

  1. HTML reports: a multi-page HTML report is written in the "coverage-report" directory, under the current working directory (a different output directory can be specified if needed). The directory is created if it doesn't yet exist; its contents are overwritten if previously generated. The report will include pages containing all Java source files covered by the test suite. By default, the tool looks for ".java" source files inside all directories of name "src" found directly or indirectly under the current working directory; any intermediate sub-directories between "src" and the top-level package directory, such as "src/java" for example, are also searched.
  2. Coverage data files: a single serialized file of name "coverage.ser" is written under the current working directory or a specified output directory. If the file already exists, its contents are either overwritten or appended with the in-memory results of the current test run, as specified.
    These files can be read and processed by external tools. The method will create a new CoverageData instance with all the coverage data available in a given serialized file.

Call points

When running a test suite with the coverage tool, there is optional "call point" information which can be gathered, as selected by the user. A call point is the point in the source test code from which a specific line of production code was exercised.

Generating coverage with this extra information takes more time and produces significantly larger output; on the other hand, it can be useful to know which lines of test code caused a given line of production code to be executed during the test run. When included in the HTML report, the list of call points appears hidden at first but can be easily viewed by clicking on each executable line of code.

Configuring the coverage tool

To enable the JMockit Coverage tool in a JUnit/TestNG test run, add jmockit.jar to the runtime classpath, and specify at least one of the "coverage-output|metrics|classes" system properties. With JUnit, make sure that jmockit.jar appears first in the classpath. (For more details on running tests with JMockit, see the corresponding section in the Tutorial.)

When not using the JMockit mocking APIs, code coverage can still be activated without adding any jar to the classpath. Instead, run with "-javaagent:<proper path>/jmockit.jar=coverage" as a JVM initialization parameter.

In most cases, the coverage tool does not require any additional configuration to be used. There are, however, several aspects of the tool's behavior which can optionally be configured for a given test run. This is done by setting one or more of several "coverage-xyz" system properties for the JVM instance running the test suite.

Note that you should be able to easily specify these properties inside an Ant target, a Maven surefire plugin configuration, or a test run configuration for your Java IDE of choice, using either JUnit or TestNG; no JMockit-specific plugin is needed.

The available configuration properties are:

  1. [jmockit-]coverage-output: one or more comma-separated values between html, html-nocp ("nocp" stands for "no call points"), serial, and serial-append, which select the kind of output to be generated at the end of the test run. The default if none is specified is to generate the basic HTML report (html-nocp).
    The "html" and "html-nocp" values are mutually exclusive, just like "serial" and "serial-append". However, it is valid to have one of each pair specified at the same time. In such a case, at the end of the test run both kinds of output will be written.
    The presence of "serial" or "serial-append" causes a serialized data file of name "coverage.ser" to be generated; in the case of "serial-append", coverage data gathered by the current test run will be appended to the contents of a previously existing data file (if said file doesn't exist, it has the same effect as "serial").
  2. [jmockit-]coverage-outputDir: absolute or relative path to the output directory, to be used for writing any "coverage.ser" or "index.html" files (plus the remaining ".html" files of the HTML report, in automatically created sub-directories). By default, the current working directory of the running JVM is used, with all ".html" files of the HTML report generated inside a "coverage-report" sub-directory.
  3. [jmockit-]coverage-srcDirs: comma-separated list of Java source directories to be searched when generating an HTML report. (This is not relevant for the serialized data file.) Each directory is specified by an absolute or relative path. If no such directory is specified, all "src" directories under the current working directory are searched.
  4. [jmockit-]coverage-classes: Either an OS-like regular expression (with the typical "*" and "?" wildcards), or a java.util.regex-conformable regular expression. The given expression will be used to select the classes (by fully qualified name) from production code which should be considered for coverage. By default, all classes in production code loaded during the test run and which are not inside jar files are considered.
    For example, "some.package.*" selects all classes under some.package or any sub-package.
    As a special case, if the property is specified as "loaded", then all classes will be considered, but only those which get loaded by the JVM during the test run; classes that are part of the codebase but never get loaded are left out. This is very useful when the test run includes only a few tests, targeting only a subset of the codebase.
  5. [jmockit-]coverage-excludes: The same as the previous property, but for class names which should be excluded from consideration when instrumenting classes for coverage. This property can be used together with coverage-classes or on its own. By default, no classes between those selected for coverage are excluded from consideration.
  6. [jmockit-]coverage-metrics: one or more comma-separated words between line (the default), path, data, and all, which select the specific set of code coverage metrics to gather coverage information for.
  7. [jmockit-]coverage-check: one or more semicolon-separated rules specifying minimum coverage checks to be performed at the end of a test run. By default, no such checks are performed. For details, see the Checking minimum coverage section.

Aggregated reports for multiple test runs

When the coverage tool generates a report at the end of a test run, it always overwrites any previous report. Normally, the coverage data from which the report is generated reflects only what was gathered during the current test run. Now suppose you have multiple test suites or test run configurations, and you want to generate a single aggregated HTML report for the code covered by the full set of tests. Here is where the "coverage.ser" serialized data files come in.

To activate the generation of these files, we simply set the coverage-output system property to a value containing "serial" or "serial-append". As these two values suggest, there are different ways to combine multiple coverage data files. The following sub-sections provide the details for each case.

Generating an aggregate report from multiple data files

Suppose we want to gather coverage data from multiple test runs and later generate an aggregate HTML report merging together the results from all test runs. Each test run needs to generate its own coverage.ser file, so that later they can be merged together in a final step which produces the report; therefore, each test run should be configured with "coverage-output=serial". Note that, in order to preserve the original coverage.ser output files generated by each test run, they will need to be written or copied into different output directories.

Assuming that two or more coverage.ser files are available in separate directories, an aggregate report can be generated from them by executing the mockit.coverage.CodeCoverage.main method (a regular Java "main" method). To facilitate this, the jmockit.jar file is executable. As an example, the following Ant task could be used:

<java fork="yes" dir="myBaseDir" jar="jmockit.jar">
   <jvmarg line="-Dcoverage-output=html"/>
   <arg line="module1-outDir anotherOutDir"/>

The example above uses "myBaseDir" as the base directory where a separate JVM instance will run. Two output directories containing "coverage.ser" data files are specified, as command line arguments. Other configuration parameters can be specified through the "coverage-xyz" system properties. This separate JVM instance will read each of the "coverage.ser" data files, merge the coverage data in memory, and then generate the aggregate HTML report before exiting.

Generating an aggregate report from a single data file appended after each test run

Another way to obtain an aggregate coverage report from the execution of multiple test runs is to accumulate coverage data from all tests into a single data file. This can be achieved by using the same working directory for all test runs or by pointing coverage-outputDir to a shared directory, while having coverage-output=serial-append for each test run. Additionally, the last test run in the sequence should also specify html or html-nocp for the coverage-output property, together with serial-append. Naturally, the first test run must not read data from this file; therefore, either the file should be deleted before the first test run, or ignored by having the first test run use coverage-output=serial.

So, the difference between output modes "serial" and "serial-append" is that with the first we have multiple "coverage.ser" files (each in a different directory used by a separate test run), while with the second we share a single data file between all test runs.

Checking minimum coverage

If desired, JMockit Coverage can check that the final coverage percentages at the end of a test run satisfy arbitrary minimum values. Such checks can be specified through one or more checking rules assigned to the "coverage-check" system property (when more than one, they must be separated by ";" characters).

Each checking rule must be in the form "[scope:]min line percentage[,min path percentage[,min data percentage]]". There are three types of scopes:

All checks (if any) are performed at the end of the test run (at JVM shutdown, actually). Other forms of output (HTML report, serialized file) are not affected. When an individual check fails, a descriptive message is printed to standard output. If one or more checks have failed, two final actions are taken to have the fact reported: first, an empty file of name "coverage.check.failed" is created in the current working directory; second, an error (specifically, an AssertionError) is thrown. When checks are performed but they all pass, the "coverage.check.failed" file, if present in the current directory, is deleted.

The use of a file to mark the success or failure of coverage checks is meant to allow build tools to react accordingly, typically by failing the build when the file is present. For example, we can do the following in an Ant build script:

<fail message="Coverage check failed">
   <condition><available file="coverage.check.failed"/></condition>

Or the following in a Maven pom.xml file:


Activating coverage in a Maven project

If you run tests with Maven's "test" goal, you will need the "jmockit" dependency in the pom.xml file (assuming the "jmockit.version" property has been properly defined):


In Maven 2/3, the surefire plugin is the one usually responsible for actually running tests. To activate and configure the coverage tool, specify values for the appropriate "coverage-xyz" system properties.

         <!-- At least one of the following needs to be set: -->
         <coverage-output>html</coverage-output>     <!-- or: html-nocp, serial, serial-append -->
         <coverage-metrics>all</coverage-metrics>    <!-- or: line, path, data -->
         <coverage-classes>loaded</coverage-classes> <!-- or a "*" expression for class names -->

         <!-- Other properties, if needed: -->
         <coverage-outputDir>my-dir</coverage-outputDir>       <!-- default: target/coverage-report -->
         <coverage-srcDirs>sources</coverage-srcDirs>          <!-- default: all "src" directories -->
         <coverage-excludes>some.package.*</coverage-excludes> <!-- default: empty -->
         <coverage-check>80</coverage-check>                   <!-- default: no checks -->

Finally, if the tests don't actually use the JMockit mocking APIs, it's still possible to use the coverage tool even without the "jmockit" dependency. The only thing that is necessary is to configure the surefire plugin as follows:

         <!-- coverage properties -->

Including the HTML report in a Maven site

To have the JMockit Coverage HTML report included in the generated Maven site documentation, the src/site/site.xml descriptor file needs to be provided, with contents similar to what's shown below.

<?xml version="1.0" encoding="UTF-8"?>
      <menu ref="reports"/>
         <item name="Code Coverage Report" href="../../coverage-report/index.html"/>

Turning coverage off

To temporarily turn coverage off for a particular test run, we can set the coverage-output system property to an unknown output format, such as "-Dcoverage-output=none". The same effect will be achieved by setting "-Dcoverage-metrics=none" or "-Dcoverage-classes=none".

Another, more interactive, way is to manipulate the read-only attribute of the relevant output file, when one has already been generated. The particular file to be manipulated, always in the working directory, is "coverage.ser" for serialized output or "coverage-report/index.html" for HTML output. The file attribute is checked by JMockit at startup; when marked as read-only it cannot be overwritten, so JMockit avoids the attempt entirely. Note that the working directory can usually be selected separately for each test run configuration in the Java IDE. Also, a Java IDE usually provides an easy mechanism to toggle the read-only status of a file in the project: in IntelliJ IDEA it is done by double clicking the status bar, with the desired file opened in the editor; in Eclipse there is a "Read only" check box in the "Properties" screen (which can be opened by typing "Alt+Enter") for the text file selected in the editor.

Standalone mode

Normally, the coverage tool is used during a JUnit/TestNG test run to measure test coverage. It can also be used in standalone mode, by attaching to an arbitrary Java 6+ process to measure line and path coverage metrics on specified classes.

There are two ways to activate standalone mode. The first is to start the target JVM instance with the "-javaagent:<proper/path/>jmockit.jar=standalone" command line argument. Initial configuration settings for the coverage tool can be specified through the "coverage-xyz" system properties previously described, but this is entirely optional; the configuration properties can be modified later through a dedicated UI. The second way is to run the main method inside jmockit.jar, by running "java -jar jmockit.jar <nnn>" in the command line, where "<nnn>" is the number of the local process which is running the target JVM instance: this causes the JMockit coverage agent to be loaded into the target JVM.

Once the target process is running with the JMockit Coverage Java agent, the user can connect to it with a JMX client which can access arbitrary "MBeans". Usually, the standard JConsole tool available in a Java 6+ JDK will be used. The JMockit Coverage MBean provides several configuration properties (the same ones which can be set with "-D" on the command line), and one operation through which the desired output can be generated. The user interface provided by JConsole is shown below, where the process that is running with the coverage tool is a Tomcat 7 server instance.

The configuration properties (shown as "Attributes" of the "CoverageControl" MBean above) are as before, except in the case of "SrcDirs" (which corresponds to coverage-srcDirs). If this property is not specified, no attempt is made to find source files for the classes considered for coverage.

In case the MBean UI is not used, coverage output will be generated at JVM shutdown, according to the configuration specified through coverage properties.