Mchr3k - JaCoCo





Update site:

JaCoCo User Guide

Source Directives

The new default filters listed under Features below are always enabled.

Source directives allow coverage to be turned on/off in arbitrary sections of code. The following directives are supported:


To allow for easy migration from Clover the following directives are also recognised:


Source directives are disabled by default. To enable them you must add a new section to your jacoco coverage ant task:

    <file file="${result.exec.file}"/>
  <structure name="JaCoCo Ant Example">
      <fileset dir="${result.classes.dir}"/>
    <sourcefiles encoding="UTF-8">
      <fileset dir="${src.dir}"/>
  <html destdir="${}"/>

By default source directives will only be recognised when directives that turn off coverage are acomapnied by a comment e.g.

///COVERAGE:OFF because the following code is too simple to bother unit testing.

This requirement can be disabled by adding an extra parameter to the ant task:

    <sourcedirectives requirecomment="false"/>

EclEmma User Guide

Source Directives support can be configured through a new option in the Code Coverage preferences. This feature is disabled by default.


This fork adds the following:

  • New default filters which ignore elements of bytecode which are not useful in a coverage report:
    • Empty no args constructors including implicit no args constructors.
    • Default enum methods - values() and valueOf().
    • Synchronization exit.
  • Arbitrary sections of source code can have coverage turned off using source directives.
    • Access to this feature can currently only be enabled from ant.

Blog Posts

Further details about this project can be found on my blog:

Clover to JaCoCo Migration

Source directives are currently supported by Clover so this section discussed how to migrate your build from Clover to JaCoCo. This example also assumes you will use offline instrumentation which allows tools like Powermock to work.

These instructions assume you store the required jacoco jars in the following locations:

  • lib/build/jacocoant.jar
  • lib/test/jacocoagent.jar

Task Definitions


<taskdef resource="clovertasks" />
<typedef resource="clovertypes" />


<taskdef resource="org/jacoco/ant/antlib.xml">
  <classpath path="lib/build/jacocoant.jar"/>



<clover-setup initString="./reports/coverage/metadata/coverage.db">
  <fileset dir="./build/src" />
<clover-clean />
<javac destdir="./build/classes_instr" debug="true">
  <src path="./build/src" />
    <fileset dir="./lib" includes="*.jar" />
    <fileset dir="./lib/test" includes="*.jar" />


<instrument destdir="./build/classes_instr">
  <fileset dir="./build/classes" includes="**/*.class"/>

Running Tests

Clover - include clover.jar in junit classpath.

JaCoCo - include jacocoagent.jar in junit classpath. You also need to set a sysproperty within the junit task:

<sysproperty key="jacoco-agent.destfile" file="./reports/coverage/jacoco.exec"/>

Generating Reports


  <current outfile="./reports/coverage">
    <format type="html" orderBy="ElementsCoveredDesc" />
  <current outfile="./reports/coverage/current.xml" />


    <file file="./reports/coverage/jacoco.exec"/>
  <structure name="ProjectName">
      <fileset dir="./build/classes"/>
    <sourcefiles encoding="UTF-8">
      <fileset dir="./src"/>
  <html destdir="./reports/coverage"/>


22/02/2013 (unreleased)

  • Complete rewrite of finally block dedup to remove line number information requirement and improve accuracy.


  • Merge latest JaCoCo changes, remove local offline instrumentation implementation.


  • Fix finally block coverage.
  • Complete offline instrumentation.
  • Add tests for finally block coverage, improve comments on other tests.


  • Get offline instrumentation working.
  • Fix bug in directives handling.
  • Ignore coverage of finally exception path.


Fixed handling of source directives in light of:

  • Init methods can overlap line numbers of other methods.
  • Line numbers within methods are not always visited in order when you have try/catch/finally blocks.
  • Begin work on offline instrumentation.


Initial release with new filters.