Generate Codecoverage Report with Jacoco and Sonarqube

Teten Nugraha
Backend Habit
Published in
4 min readFeb 6, 2020
Photo by William Iven on Unsplash

Based on my previous article we talked about JUnit on Service Layer and JUnit on Controller Layer. And now, we will talk about how to generate Codecoverate Report using Jacoco plugin and Sonarqube.

As you already know, Sonarqube is for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities on 20+ programming languages.

JaCoCo is a free code coverage library for Java, which has been created by the EclEmma team based on the lessons learned from using and integration existing libraries for many years.

Prerequisite

  • You have downloaded source code on this link.
  • You have download and setting Sonarqube and you can use community edition for this one.

Check Before ..

Lets check again Unit Test that we have writed. Check on UserServiceTest and do Unit Test check.

and then, we check again UserControllerTest.

Okay, looks fine.

Start Sonarqube Server

Open your sonarqube directory and click StartSonar.batch (is depend on your operating system on your laptop)

wait for a moment until sonar server is running.

Generate Code Coverage Using Jacoco

  • Add below Jacoco configuration on pom.xml properties section
<jacoco.version>0.8.3</jacoco.version>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.language>java</sonar.language>
  • and add also Jacoco plugin still on pom.xml under <plugin> section
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<skip>${maven.test.skip}</skip>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
<output>file</output>
<append>true</append>
<excludes>
<exclude>*MethodAccess</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<phase>test-compile</phase>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
  • after that, open terminal or command prompt and direct to root directory of project. Execute this maven command to build project.

mvn clean install

wait until build process has finished like this.

Look, on coverage report under target folder there is file called jacoco-unit.exec. That file who used by Sonarqube to generate and display report about codecoverage, code quality , etc.

keep your terminal on root folder of project. and then execute this maven command to connect with Sonarqube.

mvn sonar:sonar

wait until build process has finished.

after finished, There are two link for you to open sonarqube on browser. click that link and automatically open your browser.

this page is about report from out project. Seen that Coverage around 91.5%. Unit Test 16. and if you wanto to know detail of codecoverage from whole your code, click code tab.

from that data, we can improve our code to be better again.

Conclusion

Jacoco and Sonarqube is important for many programmer. With both, we can improve quality of code little by little.

If it was interesting or helpful to you, please do press the 👏 clap button and help others find this story too or if you wanna talk internally with me , reach me in https://linktr.ee/teten_nugraha.

Did you know you could give up to 50 claps?

--

--

Teten Nugraha
Backend Habit

Software Engineer, 8 years of experience. Expertise with Microservices, Spring Boot, CICD, Docker https://www.linkedin.com/in/teten-nugraha