We have already covered how to do code quality check with Sonarqube in my previous blog. In this blog, we will be discussing how can we setup JaCoCo a code coverage tool and exports reports to SonarQube. Though report can be directly read but having reported at a single place is a good idea. So, let get into ‘JaCoCo Code Coverage And Reports With Sonarqube’
What this blog will cover.
What is JaCoCo and why you need this? Setting up JaCoCo. Importing reports to SonarQube
What is JaCoCo and why you need this?
JaCoCo is an abbreviation for Java Code Coverage. It’s free to open source library developed by EclEmma. This tool helps us in finding out the piece of code which is not used at all while running the system, It can be manual or by some sort automatic test. When we are working in the team and we have many minds handling the same project there can cases somebody has written a code which doesn’t execute at all in runtime or worst is not covered by unit test. Attaching JaCoCo to your unit test make sure you haven’t missed out any unwanted untested code. It gives you how many times your code got executed.
Setting up JaCoCo.
There are multiple ways of running JaCoCo for your project, You can pick any of them depending on your need. Don’t worry, in the end, all will generate same data which we can use in Sonarqube. We are going to export the generated data in *.exec format. There are other formats as well like HTML, zip XML and CSV.
- Install it on Eclipse.
- Install using maven.
- Install standalone JaCoCo java agent on any running java process or server.
Install it on Eclipse.
This is the easiest approach and best suited when you are developing your code and running unit test cases on the local system. Steps to install it on eclipse.
Click or drag below link in your eclipse.
Install from update sites.
Go to Help-> Install new software and put in below URL. http://update.eclemma.org
Download plugin zip and install manually.
Install using maven.
If you are already using maven with your project and want to generate reports using simple maven commands then you can go with this installation. Steps to install JaCoCo and run reports.
Open up your pom.xml and add following lines.
Run the maven goals to generate reports.
For more options visit. mvn jacoco:report For more options visit. http://www.eclemma.org/jacoco/trunk/doc/maven.html
Install standalone JaCoCo java agent on any running java process or server.
This suite best when you do want to run JaCoCo on your live or test environments directly. Also in case, you do not have test setups or local workspace setup. There is standalone JaCoCo jar which you can directly plugin in your remote Java process using command line arguments. Once the java agent is attached to your server process you can open up a port to fetch reports or you can directly go to your server and ask for report dump. This report dump can be in any format CSV, XML, *.exec, HTML or zip. In order to use it with Sonarqube, we have export the report in *.exec format.
Here are the commands you to attach JaCoCo to a running Java process. -javaagent:C:\dev\servers\jacoco-agent.jar=destfile=< path to jacoco C:\lib>jacoco.exec,append=true,includes=some.application.*
This Java argument will attach the JaCoCo agent. Once you have enabled the JaCoCo agent you need to run tests manually or automated. This will create jacoco.exec on given location but dump data only on server stop. To avoid server interruption for the report you need to enable command interface using following option. output=<cpserver>,address=*,port=<some port 6300>
Importing reports to SonarQube
I ran it on one of my old projects. It looks something like this and it looks like this. For further detail, you can click on coverage percentage.