Skip to main content
blog title image

2 minute read - Java For Testers Java Jacoco Code Coverage

Jacoco Java Coverage Reporting in Junit 4 and 5

Nov 14, 2021

Jacoco can be used for coverage reporting from maven for both JUnit 4 and JUnit 5

What is Jacoco?

Jacoco is a code coverage tool. This means that when the Java code is run you can see a report of the executed code. Normally we execute code with code coverage instrumentation during the execution of Unit tests, to help identify what code is executed during the running of those tests. Execution of code does not mean that the code was ’tested’ as it might have been executed in passing as another test was run. But it is a useful indication that some code is not even exercised during testing. It is also a very useful code review tool, when running a subset of Unit tests to make sure that we covered enough code.

This post might be useful supplemental reading where I describe the use of code coverage as a review tool.

Jacoco can be found online:

Adding Jacoco into the pom.xml build

Add Jacoco into the build section of the pom.xml

    <build>
        <plugins>

            <!-- see pom.xml for rest of build section -->

            <!-- add code coverage and make sure it covers both Junit4 and Junit5 -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.5</version>
                <!-- defaults to target/jacoco.exec -->
                <executions>
                    <!-- allow Jacoco to run -->
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <!--
                        Generate the html report for the test phase
                        https://www.eclemma.org/jacoco/trunk/doc/report-mojo.html
                    -->
                    <execution>
                        <id>report</id>
                        <!-- generate coverage report for test phase -->
                        <!-- target/site/jacoco -->
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

And add Jacoco to the reporting section

   <reporting>
        <plugins>

           <!-- see pom.xml for rest of reporting section -->

           <!-- generate the coverage report using Jacoco -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <reportSets>
                    <reportSet>
                        <reports>
                            <!-- select non-aggregate reports -->
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>

Notes

I kept this as simple as possible with no changing of the configuration.

I didn’t add any build failing checks on coverage percentages into the build.

To run the build and generate the code coverage report use:

mvn clean test site

Then the Jacoco report will be in the Project Reports of the site

  • target/site/project-reports.html

Supporting Source Code

You can find the supporting source code for this over at:

And specifically:

You can find more code coverage examples in:

I orginally wrote this on 2020-06-22, but forgot to take it out of draft mode.