Jacoco and Sonar with Kotlin

When we began a new project recently the team thought about using Kotlin over Java to implement the backend. The project lead had issues with this because there was no clear information on how Kotlin would be supported by tools like Sonar and Jacoco. Since these tools deliver important information about code quality and potential issues we decided to spend some time on evaluating how these tools would collaborate with Kotlin.

 Sample project

The sample project is a simple calculator which basically supports the four arithmetic operations (add, subtract, multiply, divide). The exceptional case “division by zero“ is also handled. I created the application, some unit tests and also some integration tests. The app is deployed into a JBoss-EAP-Environment and publishes the required REST endpoints. The main target was to get Jacoco to analyse the code coverage, unit tests and integration tests combined. Then Sonar was supposed to display this information properly as it does with Java code. Please find the sample project on Github.

In the sample project I use Maven to build the app. As a consequence, any configuration that is necessary to achieve our goal is done in Maven. Please find more information on how to configure Jacoco and Sonar in the sample project.


Since Jetbrains does not yet provide a Kotlin plugin for Sonar, we use the following plugin: https://github.com/arturbosch/sonar-kotlin.

It was easy to install and I did not have any trouble with it. The plugin comes with a preset of rules that can be extended at will. In the sample project I left the rules unchanged for simplicity reasons. I added a class that intentionally violates some of these rules in order to find out how Sonar would display such violations. The result can be seen in the following pictures:




Code coverage

After we configured Jacoco appropriately we had the Jacoco files that contain code coverage data but still had to display them properly in Sonar. This task is also handled by the previously installed Kotlin plugin. I was not sure what Sonar was able to display concerning covered lines. I wanted it to be Kotlin code of course but feared that it would be some kind of reverse compiled Java code. As you can see below, it is Kotlin code which made our project lead and our development team very happy.


We finally convinced our project lead that Kotlin would fit very well for this project and we are happy to being able to use it.