Unit Tests Coverage and Source Code Analysis for Spring Boot Microservices with SonarQube
This article is to explain how we can integrate SonarQube to check the unit test coverage and source code analysis through Jenkins. Here we are going to create a Spring Boot microservice and write unit tests to our controllers and while building through Jenkins using gradle, going to push the data to SonarQube using the “sonarqube” plugin for Gradle and then will do the analysis from SonarQube UI.
1. What we are going to setup
2. Setting up SonarQube
When we take SonarQube it has mainly two components.
- SonarQube Server: Which resides with Compute Engine, Search Server and a Web Server.
- SonarQube Database: Which helps to store the configuration of the SonarQube instance and the quality snapshots of the projects.
In this setup I’m going to setup SonarQube server along with the PostgreSQL database. Note that SonarQube can support Microsoft SQL Server and Oracle as well. To get more understanding on the versions that supported, refer this.
Installation of PostgreSQL
Follow the below steps, to start and create a user for SonarQube.
Now we need to setup the database schema for the SonarQube. To do that follow the below steps and execute the commands.
Installation of SonarQube
Download the latest version.
Extract and rename the folder.
Add a user and group to run as a separate user for SonarQube.
Connect SonarQube to Database
Go to sudo vim /home/ajanthan/applications/sonarqube/sonarqube/conf/sonar.properties
Go to sudo vim /home/ajanthan/applications/sonarqube/sonarqube/bin/linux-x86–64/sonar.sh
To run as service,
Go to sudo vim /etc/systemd/system/sonar.service and add the below:
To get started execute the below commands, sometimes if you are using a different java installation directory then you need to update the below file as well.
Go to sudo vim /home/ajanthan/applications/sonarqube/sonarqube/conf/wrapper.conf
Execute the below command to start and check the status.
Go to http://localhost:9000 then you will directed to the login page and after entering the default admin:admin then you can get into the console.
3. Setting up the Microservice Project
The option from the Spring Boot itself to write unit tests is to write based on the spring-boot-starter-test dependency. This includes all dependent libraries we use when we are writing unit tests. If we need any other libraries apart from this we can add them as well as dependencies.
JUnit 5: The de-facto standard for unit testing Java applications.
Spring Test & Spring Boot Test: Utilities and integration test support for Spring Boot applications.
AssertJ: A fluent assertion library.
Hamcrest: A library of matcher objects (also known as constraints or predicates).
Mockito: A Java mocking framework.
JSONassert: An assertion library for JSON.
JsonPath: XPath for JSON.
Also take a note on WebClient unit tests, earlier we used the mocking the WebClient calls using Mockito library but there is better option using the MockWebServer, this has been discussed here.
Configurations at build.gradle
Mainly two plugins used here, jacoco to generate the code coverage reports that can be read by the SonarQube and sonarqube plugin to push the Code Analysis Configurations.
This will enable the xml based reports, that can be viewed under build directory.
This is to mention that after we execute the ./gradlew test invoke the jacocoTestReport task to generate the reports.
Finally the below config to pass to SonarQube, mentioning the exclusions and the path of the report.
4. Setting up Jenkins Pipeline
Use the below pipeline script to create the pipeline.
5. Verify in SonarQube
Now log into the SonarQube instance and view the project and check the statistics.