Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage.
In this article, i am going to show you how to use Cobertura for maven based java application for measuring the code coverage by test cases.
Cobertura Code Coverage Report
Go to the root of the project and run the following command that will analyze the code coverage with Cobertura and generate the output report (showing the detailed analysis of coverage).
mvn cobertura:cobertura
Accessing the report
The generated code coverage analysis report can be accessed through ${project}/target/site/cobertura/index.html
In this article, i am going to explain how to use SonarQube and JaCoCo as a Code Coverage and Source Code Quality analysis tool for Spring Boot application.
What is Code Coverage and why it is important?
Code Coverage is an important topic when it comes to Test Driven Development (TDD). most of the developers are curious to know how percentage of source code is covered with test cases developed (for both unit and integration tests).
Code Coverage shows the stats of how much of source code is covered and tested with test cases (both unit and integration) developed for the application. Therefore the code coverage analysis is an important fact of measuring the quality of the source code. we need to write the test cases to achieve higher code coverage which will increase the maintainability of the source code.
Technology Stack
The following technologies will be used for this article.
SonarQube
Docker
JaCoCo
Spring Boot Application with maven
Install and Run SonarQube with Docker
The most of the developers know the “SonarQube” as a code quality analysis tool. This has the capability of the executing Unit and Integration tests with given library/tool (such as Cobertura, JaCoCo etc..) and it gives a detailed analysis of code coverage of the source code. In this article, we will run SonarQube as a docker image. Therefore we need to have docker installed in our development environment.
If you do not have SonarQube in your local development environment, you can download it with following command.
docker pull sonarqube
Once the SonarQube docker image is retrieved, it can be run with following command.
docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube
This will start a docker container based on the sonarqube image and give it the name sonarqube. Adding the -d means the container will run in detached mode (background). The -p 9000:9000 and 9092:9092 means that we expose port 9000 and 9092 to the host using the same port numbers.
Now you can navigate to http://localhost:9000 and you will see your local SonarQube dashboard.
JaCoCo Maven configuration
JaCoCo is a one of the famous code coverage library available for java based applications. In oder to add JaCoCo for the project, you have to add the following maven plugin (under the plugins) for the pom.xml of the project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
(This should be added under the plugins section of the pom.xml of the project)
JaCoCo Test Coverage Analysis with SonarQube
First you need to run the test cases with maven before sending the report for the Sonar server. This can be done with following command.
mvn test
SonarQube has a really good integration with test code coverage. It allows you to analyze which parts of the code should be better covered, and you can correlate this with many other stats. If you want to send your report to your Sonar server the only thing you need to do is to execute the following command in the terminal. (make sure that you have run the mvn test command successfully before executing the below command)
Then it will send the inspection report to the SonarQube and you can access thedetailed report through http://localhost:9000 using the specified login credentials.
username : admin
password : admin
Run as a Single Command
As you can see that we have used two separate commands for integrating test result analysis with sonar.
Both of above commands can be composed into one single command as follows.
mvn test sonar:sonar -Dsonar.login=admin -Dsonar.password=admin
Exclude Classes from Code Coverage Analysis
In the code coverage analysis we focus only about the classes that should be covered with unit and integration tests. that mens the controllers, repositories, services and domain specific classes. There are some classes which are not covered by either unit or integration tests. In order to get the correct figure of code coverage analysis, it is required to exclude those non related classes when performing code coverage analysis.
E.g:- configuration related classes (SpringBootApplication configuration class, SpringSecurityApplication configuration class etc..) should be avoided
This can be done with adding the classes as classes to be excluded under the “properties” section of pom.xml.
You can add multiple exclusions and each of them should be separated by comma. According to the above configuration, SpringBootDockerExampleApplication and any class under the config package will be excluded/ignored when performing code coverage analysis.
In the code coverage analysis we focus only about the classes that should be covered with unit and integration tests. that mens the controllers, repositories, services and domain specific classes. There are some classes which are not covered by either unit or integration tests. In order to get the correct figure of code coverage analysis, it is required to exclude those non related classes when performing code coverage analysis.
E.g:- configuration related classes (SpringBootApplication configuration class, SpringSecurityApplication configuration class etc..) should be avoided
This can be done with adding the required classes as excluded list under the “properties” section of pom.xml.
You can add multiple exclusions and each of them should be separated by comma. According to the above configuration, SpringBootDockerExampleApplication and any class under the config package will be excluded/ignored when performing code coverage analysis.