Skip to main content

Featured

Java Interview Questions for Senior Full Stack Developer

Subscribe to our YouTube Channel to know interview questions

Part 2 - Integrate JaCoCo plugin with Sonar and Maven for Code Coverage


What have we learned so far

1. Sonar Server Configuration
PART 1 : SETTING UP SONAR SERVER LOCALLY


JaCoCo Integration with Maven and Sonar for Code Coverage


This post will explain the Steps to integrate JaCoCo plugin with Sonar and Maven for Code Coverage.
With this integration you will be able to see below details on Sonar dashboard:
1. Bugs
2. Vulnerabilities
3. Debt
4. Code Smells
5. Code Coverage
6. Unit tests
7. Duplicate Code Percentage
8. Duplicate Code Block

JaCoCo


JaCoCo is a free code coverage library for Java, which has been created by the EclEmma team based on the lessons learned from using and integration existing libraries for many years.
To understand more about JaCoCo  click here

Prerequisite: we should have Sonar server up and running with the token. to know about this you can follow my previous blog:

Once the above steps are completed successfully, follow the below steps to configure JaCoCo with Maven & Sonar
For this process, we will use a project from below git repository:
https://github.com/onlyfullstack/scalable-web-json-comparator 

clone this git project into your local system and follow below steps:
Step-1 : Add below JaCoCo configuration in the pom.xml file under properties section
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.language>java</sonar.language>
<jacoco.version>0.7.9</jacoco.version>

Step-2 : Add below JaCoCo plugin in the pom.xml file under Plug In section
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <configuration>
        <skip>${maven.test.skip}</skip>
        <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
        <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
        <output>file</output>
        <append>true</append>
        <excludes>
            <exclude>*MethodAccess</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <phase>test-compile</phase>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>verify</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Step-3 : Execute below mvn command from project root folder(where pom file exist)

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=false

This command will create JaCoCo reports for us which will contain the code coverage reports at scalable-web-json-comparator\scalable-web\target\coverage-reports\ path.
the code coverage report will be in jacoco-unit.exec file.


Step-4 : Execute below mvn command to push the sonar report to our sonarQube server.


mvn sonar:sonar -Dsonar.jacoco.reportPaths=target/coverage-reports/jacoco-unit.exec
-Dsonar.projectName="scalable-web" -Dsonar.projectKey="scalable-web" 
-Dsonar.host.url=http://localhost:9000
 -Dsonar.login=1c01ff3138588827f552cc6e7d4971ed004f5874
sonar.projectName - Project name
sonar.projectKey - Project key
sonar.host.url - Sonar host url
sonar.login - Carries the token which was generated in the Sonar, refer previous post to know more.
sonar.jacoco.reportPaths - JaCoCo report to get the code coverage


Step-5 : Once above build is successful, launch Sonar dashboard using the below URL and verify code coverage and unit test case percentage.
http://localhost:9000



With these steps, we have successfully integrated the JaCoCo plugin with Sonar and Maven for Code Coverage.

Let's go to our next tutorial where we will discuss below points :

- What is Eclemma?
- How to find out the code coverage in eclipse?
- EclEmma Configuration in Eclipse

Comments

  1. Hello,

    I have a project which has both Scala and Java code. I'm using Jacoco plugin for code coverage.I could see both java and scala test cases in Jacoco report but my sonarQube analysis is only picking up Java code coverage and not Scala code coverage. Could you please guide in how to configure SonarQube for Scala code coverage with Jacoco reports?

    ReplyDelete

Post a Comment

Trending Tutorials

Trending Tutorials

WebSocket With Spring Boot and Angular 6 Tutorial

In this tutorial, we will understand What is WebSocket? Why to use WebSocket? Polling vs Server Sent Events vs WebSocket and WebSocket Example with Spring Boot + Angular 6 + STOMP

Code Coverage with JaCoCo Sonar and Maven

In this tutorial, we will understand the importance of code coverage and how can we setup the Sonar + JaCoCo + Maven to get the code coverage. We will look into Setting up Sonar Server Locally, Integrate JaCoCo plugin with Sonar and Maven for Code Coverage, Find Code Coverage in Eclipse : EclEmma Configuration

Java 8 Features

Learn all about Java 8 features in details

Lambda Expression in Java 8

Ever wonder what Lambda Expression is? What Lambda expressions are compiled to? if no then you should go through this tutorial to know more about Lambda and how they are different than anonymous class.

Popular Posts