Generate Codecoverage Report with Jacoco and Sonarqube

Generate Codecoverage Report with Jacoco and Sonarqube

Image for postPhoto 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.

Image for post

and then, we check again UserControllerTest.

Image for post

Okay, looks fine.

Start Sonarqube Server

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

Image for post

wait for a moment until sonar server is running.

Image for post

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.

Image for post

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.

Image for post

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.

Image for postImage for post

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.

Image for post

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.

Did you know you could give up to 50 claps?

16

No Responses

Write a response