TestNG Unit Test Coverage by SonarScanner, SonarServer and JaCoCo

Senthilkumar.P
2 min readSep 23, 2022

--

TestNG, SonarQuebe and JaCoCo

Introduction

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.

What is Code Coverage?

Code coverage is a measure of how much for your application’s code has been executed in testing. This covers not only seeing which lines of code have been executed but also checking that all branches have been covered. Code coverage is also called Test coverage. This gives developer teams reassurance that their programs have been broadly tested for bugs and should be relatively error-free.

Prerequisite

  • AWS EC2 Instance
  • Jenkins
  • SonarQube

Application Deployment

Step: 1

We have this Sample Application in below GitHub repository,

Step: 2

Dependencies

To get this up and working we need to add the Jacoco maven plugin as shown below.

Note: Mention Unit Test coverage ration on below line in maven pom.xml file. Our Scenario we have set code coverage ratio is 0.8

<limits>

<limit>

<counter>LINE</counter>

<value>COVEREDRATIO</value>

<minimum>0.8</minimum>

</limit>

</limits>

Step: 3

Create Jenkins Pipeline

Create below script to execute unit test from Jenkins Pipeline,

Step: 4

Go to Jenkins and execute pipeline, successfully executed Jenkins Pipeline.

Pipeline Report

Step: 5

Go to SonnarServer to see Unit Test coverage report,

JaCoCo Unit Test Coverage

Here, we have covered unit test on only 0.8% only and we have left 0.2% is an optional.

Step: 6

Now if we go to report page we can see

JaCoCo Test Coverage

Here, we didn’t written unit test for method addtionNum, now JaCoCo analyzed our code and it shows message like “Not covered by tests”.

Conclusion

We have successfully created TestNG Unit Test Coverage by SonarQube, JaCoCo and generated code coverage report. Hope you find this article useful.

--

--

No responses yet