Back

June 13, 2022

Code Coverage With IntelliJ, Jacoco And SonarQube

A quick guide on how to check for code coverage using IntelliJ, Jacoco, and SonarQube.

Overview

  1. IntelliJ
  2. Jacoco and SonarQube

IntelliJ

  1. Run configuration
  2. Inclusions and exclusions
  3. Results

Run Configuration

You can select a package or class to run tests with coverage. A run configuration is automatically created which you can then modify as necessary.

Inclusions and Exclusions

  1. Open up the run configuration that has been created
  2. Select the ‘Code Coverage’ tab
  3. Specify the class or package to include and exclude accordingly

Results

Run tests with coverage:

  1. Open up IntelliJ File Explorer
  2. Right click on desired class or package
  3. Select Run tests with coverage

Coverage console

  1. Once the run is completed, the Coverage console should appear on a right pane
  2. This console shows the coverage percentages by methods and lines
  3. You can navigate up and down the directory tree to get the coverage of each specific package and class

File explorer

  1. On the file explorer, the class method and line coverage percentages are also displayed on each level of package and class.

Jacoco and SonarQube

  1. Maven configuration
  2. Inclusions and exclusions
  3. Results

Maven Configuration

Add the following dependency in pom.xml:

<dependency>
    <groupId>org.jacoco</groupId> 
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.6</version>
</dependency>

Inclusions and Exclusions

Wildcard syntax:

  1. ** matches zero or more directories
  2. * matches zero or more characters
  3. ? matches a single character
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    
    <configuration>
        <includes>
            <include>**/com/tech/payment/**/*</include>
        </includes>
        <excludes>
            <exclude>**/com/tech/payment/config/**/*</exclude>
            <exclude>**/com/tech/payment/constants/*Enum.class</exclude>
            <exclude>**/com/tech/payment/kafka/**/*</exclude>
        </excludes>
    </configuration>
</plugin>

Here, we are:

  1. Including any file in any sub-package under the sub-package com.tech.payment
  2. Excluding any file in any sub-package under the sub-package com.tech.payment.config
  3. Excluding any file ending with Enum.class in any sub-package under the sub-package com.tech.payment.constants
  4. Excluding any file in any sub-package under the sub-package com.tech.payment.kafka

Results

Run mvn clean package

Jacoco site report

  1. Navigate to target/jacoco/site folder
  2. Open index.html with a web browser
  3. Test coverage percentages are shown by package and class level

SonarQube report

  1. Navigate to your hosted SonarQube site (e.g. localhost:9000)