This tutorial is going to cover how to integrate JUnit 5 with JaCoCo and SonarQube in Maven builds.

1. Prerequisites

  • JUnit 5 requires Java 8, you should make sure you have it ready in your environment.
  • Maven, Sonar server is available in your environment.

2. Integrate JUnit 5 With JaCoCo and SonarQube In Maven Builds

2.1. JUnit 5 Maven Dependencies

Let’s have a quick look at Maven dependencies for JUnit 5 as follows:

You can read the following tutorials to set up JUnit 5 with Gradle and Maven:

2.2. Configure JaCoCo Maven Plugin

Let’s see how we can configure the JaCoCo Maven plugin as below:

2.3. Configure Sonar with Maven

2.3.1. Prerequisites

  • Maven 3.x
  • SonarQube is already installed
  • At least the minimal version of Java supported by your SonarQube server is in use (Java 8 for latest LTS)
  • The language plugins for each of the languages you wish to analyze are installed

2.3.2. Initial Setup

To trigger SonarQube analysis on Maven projects, we can use the sonar-maven-plugin which can be run by simply executing the mvn sonar:sonar command. However, to make sure Maven can reliably resolve the sonar plugin prefix to the sonar-maven-plugin, we should set the plugin prefix for it, for example:

We have just set the plugin prefix globally by editing the settings.xml file, located in $MAVEN_HOME/conf or ~/.m2 directories In addtion, we also specified optionally the SonarQube server URL.

2.3.3. Configuring the SonarQube Analysis

Analysis parameters are listed on the Analysis Parameters page. We have to configure them in the <properties> section of your pom.xml, for example, we will set the address of SonarQue by:

2.3. The Full POM.xml

The full pom.xml file can be found on my Github project.

2.4. Sample Unit 5 Tests

In this section, we will get through some sample JUnit 5 tests that we will use to demo how to integrate JUnit 5 with JaCoCo and SonarQube in Maven builds.

Observe that the test class only has two test methods: calculateWithValidSalaryTest and calculateWithInValidSalaryTest.

2.5. Generate JaCoCo Report In Maven Builds

To generate JaCoCo report in Maven builds, we simply need to go to the project directory and run the following command:

Let’s see the latest sample output:

We can see that there are 2 tests were run, and jacoco-maven-plugin was executed. Now let’s see the result generated from the plugin by taking a look at the jacoco-ut directory in the target parent directory:

JUnit 5 With JaCoCo and SonarQube In Maven Builds - output directory

JUnit 5 with JaCoCo – Output directory

Let’s take a look at the summary report of all packages reported by the plugin:

JUnit 5 With JaCoCo and SonarQube In Maven Builds - report summary

JUnit 5 with JaCoCo and Maven builds – Report Summary

Next, let’s see a detail report of a sample class:

JUnit 5 With JaCoCo and SonarQube In Maven Builds - output class detail report

JUnit 5 with JaCoCo in Maven builds – detail output report of a sample class

We can see that the total coverage of the class is 92%, the getBasicSalary method gets 0% coverage. If you’re opening the HTML report, you can click on any method to go to coverage detail of each line of the method. For example, let’s see the detail of the getBasicSalary method:

JUnit 5 With JaCoCo and SonarQube In Maven Builds - code coverage report

JUnit 5 with JaCoCo In Maven builds – coverage report of method

We can see the line #7 which is being highlighted in red, is not covered by out JUnit tests.

2.6. Analyzing a Maven Project

Analyzing a Maven project consists of running a Maven goal: sonar:sonar in the directory where the pom.xml file sits, for example:

Let’s see an example of analysis report on the SonarQue server:

JUnit 5 With JaCoCo and SonarQube In Maven Builds - Sonar Analysis Report

JaCoCo and SonarQube In Maven Builds – Sonar Analysis Report

We can see that there are 2 Unit tests and coverage is 90.9%.

3. Conclusions

The tutorial has illustrated us how to integrate JUnit 5 with JaCoCo and SonarQube in Maven builds. The key steps including configuring the plugins: jacoco-maven-plugin, sonar-maven-plugin, installing the SonarQue server and executing the test and generate the analysis reports.

The sample source code presented in the tutorial is available on my Github project. It’s is a Maven based project and full configuration; it’s easy for referencing and verifying.

Below are other related tutorials:

JUnit 5 Tutorial

JUnit 5 and Spring Boot Example

JUnit 5 Basic Introduction

JUnit 5 Annotations Example

JUnit 5 Assertions Example

JUnit 5 Disable or Ignore A Test

JUnit 5 Exception Testing

JUnit 5 Test Suite – Aggregating Tests In Suites

JUnit 5 Assumptions With Assume

JUnit 5 Nested Tests Examples

JUnit 5 Maven Example

JUnit 5 with Gradle Example

JUnit 5 Parameter Resolution Example

Timeout Test in JUnit 5

 

 

0 0 vote
Article Rating