How to Configure Bamboo and SonarQube to Use JaCoCo

Code Coverage Nightmare With this blog I want to share with you my opinion about using  SonarQube for observing your project. It’s a really good idea to watch all teamwork related to commits, unit test results, bugs, issues and vulnerabilities visualized  in one single dashboard. In а few minutes you can download, configure and run […]

by Teodor Ognianov

January 18, 2017

3 min read

9 - How to Configure Bamboo and SonarQube to Use JaCoCo

Code Coverage Nightmare

With this blog I want to share with you my opinion about using  SonarQube for observing your project. It’s a really good idea to watch all teamwork related to commits, unit test results, bugs, issues and vulnerabilities visualized  in one single dashboard.

1

In а few minutes you can download, configure and run SonarQube, connect it to your Bamboo server and show reports. I have wasted days to research how to configure the code coverage functionality in my SonarQube server and I would like to share this research with you.

What Is Coverage Report

2

The Coverage report is the one which can help every product owner check in а few minutes how many unit tests are implemented for the project. Below the table you can see the chart showing the complexity of each class in your project and the unit test coverage.

The most used tool for showing coverage is JaCoCo reports.

Configuring Project

First Of All (And Most Important)

You need to commit a sonar-project.properties file even with one parameter. If this file doesn’t exist in your project, Bamboo will get the default .properties file and will replace all your command line parameters described below!

#—– Default SonarQube server

sonar.binaries=InvoiceManagement/InvoiceManagementModel/classes

sonar.junit.reportsPath=“target/surefire-reports”

sonar.jacoco.reportPath=”target/jacoco.exec”

sonar.java.coveragePlugin=jacoco

sonar.java.binaries=InvoiceManagement/InvoiceManagementModel/classes

Test Classes

First of all you need to write some Unit tests which will prepare the coverage report. In my application I created a different Tests Project where I placed all the unit tests.

For this example I created a simple unit test for testing a DTO class with three getter methods and one constructor.

3

You need to add one Tests class with annotations to the tests classes you want to test. As you can see it’s not necessary to create the tests classes in the tests module.

4

Ant Build

Then you can use your preferred build tool (In my case it is Ant) to do the tests.

Execute the next steps:

  1. Add new build file;
  2. Configure the JaCoCo;
  3. Build the project (It’s important because in other case the build will fail);
  4. Build the tests;

5
6

7

Configuring Bamboo

You need to create a build plan with configured task for the sonar dashboard:

8

  1. Source Code Checkout – this is GIT or other subversion control task for cheking out the latest version of your source code;
  2. Script – I had to do project build for creation of all .classes files. My set of commands is:
  • cd InvoiceManagement
  • export bamboo_release=`echo “${bamboo.planRepository.branch}” | cut -d/ -f2`
  • /opt/Oracle/Middleware-11.1.1.7/jdeveloper/jdev/bin/ojdeploy -buildfile ojdeploy-DataServices.xml -basedir ${bamboo.build.working.directory}/InvoiceManagement -define customdir=${bamboo.build.working.directory}/InvoiceManagement,version=$bamboo_release-${bamboo.buildNumber}
  1. Ant – This is the Ant task with the next file and target. Here is very important to put the Specify custom results directories property with your tests directory:
    a. Build file:
    ${bamboo.build.working.directory}/InvoiceManagement/ModelTests/build.xml
    b. Target: clean test -f
    ${bamboo.build.working.directory}/InvoiceManagement/ModelTests/build.xml
    -DjavacDebug=on -DoracleHome=/opt/Oracle/Middleware/jdeveloper/
    -DoutputDir=${bamboo.build.working.directory}/InvoiceManagement/ModelTests/classes
    -DjavacDeprecation=off
    -DjavacNowarn=off -DmiddlewareHome=/opt/Oracle/Middleware/
    c. Specify custom results directories:
    InvoiceManagement/ModelTests/TEST-com.my.project.model.AllInvoiceAppModuleTests.xml
  2. SonarQube Runner – this is the task which holds the magic. The most important part is the Additional parameters section:

You have to commit sonar-project.properties in your project!

      -DbuildNumber=${bamboo.buildNumber}

#The build number

      -Dsonar.binaries=InvoiceManagement/InvoiceManagementModel/classes

#The folder with the compiled files

      -Dsonar.junit.reportsPath=”target/surefire-reports”

#the folder with coverage reports

      -Dsonar.jacoco.reportPath=”target/jacoco.exec”

#file with the coverage report

      -Dsonar.java.coveragePlugin=jacoco

#the name of used plugin

      -Dsonar.java.binaries=InvoiceManagement/InvoiceManagementModel/classes

#The folder with the compiled files (for comparisson)

Benefits

The following points are my personal benefits which I have discovered during my Code Coverage task with SonarQube:

    1. You are stimulating your team to write Unit Tests;
    2. You are checking if your team writes some Unit Tests;
    3. You are checking if these Unit Tests are successful;
    4. You are checking your team’s code quality (Sonar’s quality profiles are well done and you can include/exclude some rules for the team/project);
  • Write your benefit here.

Conclusion

SonarQube is a great tool and it should be part of the software stack in every professional team.

Qualified IT professional focused on software development having rich background with Oracle technologies. Deep knowledge in Business Intelligence, It project management. Familiar with broad set of Java EE frameworks, including Oracle ADF, database servers as Oracle Database (9i, 10g, 11g), Database Replications, Heterogeneous Service. Teodor is a team player with good coaching skills, providing assistance with patience and deep understanding of knowledge sharing. Responsible and highly motivated.