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:
1 2 3 4 5 6 |
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <dataFile>target/jacoco.exec</dataFile> <!-- Sets the output directory for the code coverage report. --> <outputDirectory>target/jacoco-ut</outputDirectory> </configuration> </execution> </executions> <configuration> <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> </systemPropertyVariables> </configuration> </plugin> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<settings> <pluginGroups> <pluginGroup>org.sonarsource.scanner.maven</pluginGroup> </pluginGroups> <profiles> <profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <!-- Optional URL to server. Default value is http://localhost:9000 --> <sonar.host.url> http://myserver:9000 </sonar.host.url> </properties> </profile> </profiles> </settings> |
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:
1 2 3 |
<properties> <sonar.host.url>http://192.168.1.15:9000/sonar</sonar.host.url> </properties> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
public class BasicSalaryCalculatorTest { private BasicSalaryCalculator salaryCalculator; @BeforeEach void init() { salaryCalculator = new BasicSalaryCalculator(); } @Test void calculateWithValidSalaryTest() { double basicSalary = 4000; salaryCalculator.setBasicSalary(basicSalary); double expBasicSalary = basicSalary * 0.25; assertEquals(expBasicSalary, salaryCalculator.getSocialInsurance()); double expAddBonus = basicSalary * 0.1; assertEquals(expAddBonus, salaryCalculator.getAdditionalBonus()); double expGross = basicSalary + expBasicSalary + expAddBonus; assertEquals(expGross, salaryCalculator.getGrossSalary()); } @Test void calculateWithInValidSalaryTest() { double basicSalary = -100; assertThrows(IllegalArgumentException.class, () -> { salaryCalculator.setBasicSalary(basicSalary); }); } @AfterEach void tearDown() { salaryCalculator = null; } } |
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:
1 |
mvn test |
Let’s see the latest sample output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.howtoprogram.junit5.BasicSalaryCalculatorTest Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.273 sec - in com.howtoprogram.junit5.BasicSalaryCalculatorTest Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- jacoco-maven-plugin:0.7.9:report (post-unit-test) @ junit5-maven-jacoco-example --- [INFO] Loading execution data file D:\junit5-maven-jacoco-example\target\jacoco.exec [INFO] Analyzed bundle 'junit-maven-jacoco-example' with 1 classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.187 s [INFO] Finished at: 2017-09-14T22:52:13+07:00 [INFO] Final Memory: 22M/265M [INFO] ------------------------------------------------------------------------ |
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 – Output directory
Let’s take a look at the summary report of all packages reported by the plugin:

JUnit 5 with JaCoCo and Maven builds – Report Summary
Next, let’s see a detail report of a sample class:

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 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:
1 |
mvn sonar:sonar |
Let’s see an example of analysis report on the SonarQue server:

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 and Spring Boot Example
JUnit 5 Disable or Ignore A Test
JUnit 5 Test Suite – Aggregating Tests In Suites
JUnit 5 Assumptions With Assume
JUnit 5 Parameter Resolution Example