Jacoco code coverage with maven

Jococo Code Coverage in maven project

Jococo maven plugin

We are going to use jococo maven plugin to run tests on a project. We can add the plugin in the pom.xml file

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <!-- attached to Maven test phase -->
                <execution>
                    <id>report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Run the test goal after the plugin is added. The coverage is based on the tests. So it is important to write good test cases to provide maximum coverage.

mvn clean test

Reading the coverage report

The above Jococo plugin is executed during the test goal. It will generate the tests in \target\site\jacoco

Open the index.html in your browser to check the coverge. The full report can be viewed in the browser.

Running Code Coverage in Intellij

Run the tests with coverage. Select Run 'All Test' with Coverage.

Check code coverage in intellij

See the results in the IDE

Error without Jacoco

no plugin found for prefix 'jacoco' in the current project and in the plugin groups

mvn clean test jacoco:report