Azure Devops Pipeline: Code coverage Report for Spring boot using Jacoco

Deepan Kumar Manivannan
2 min readMar 28, 2021

Kent Beck on Unit Testing

From Code Complete — Steve McConnell

Minimise gap between defect insertion and defect detection

  • Unit Test
  • Assertions
  • Formal code inspection
  • Pair Programming
  • Single Stepping through debugging

All of it complements each other.

Code coverage metrics help the team to keep an eye on the part of code that is being tested and not being tested. So that team can take the necessary steps to keep it under the preferable threshold. Increases transparency and accountability.
Azure pipeline provides out of box tools to publish code coverage reports. It can be achieved by just adding a couple of lines in the azure pipeline YAML config.

Maven configuration

Maven goal to run the test

  • In spring boot project, add spring boot test starter.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
  • Set the phase of Jacoco plugin to `Test` so that maven goal — `Test` runs the tests as well as the jacoco code coverage
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<id>coverage-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Azure Pipeline configuration

  1. Task to run the tests — task: Maven@3
  2. Task to publish the test results — PublishTestResults@2
  3. Task to publish the code coverage — PublishCodeCoverageResults@1
# azure-pipelines.yml
# https://docs.microsoft.com/azure/devops/pipelines/languages/java
stages:
- stage: Build
displayName: Build stage
jobs:
- job: CodeCoverage
displayName: Code Coverage
pool:
vmImage: "ubuntu-16.04"
steps:
- task: Maven@3
displayName: "Maven Test"
inputs:
mavenPomFile: "pom.xml"
goals: "test"
- task: PublishTestResults@2
displayName: "Publish test results"
inputs:
testResultsFormat: "JUnit"
mergeTestResults: true # Optional
- task: PublishCodeCoverageResults@1
displayName: "Publish publish code coverage"
inputs:
summaryFileLocation: $(System.DefaultWorkingDirectory)/target/site/jacoco/jacoco.xml
pathToSources: $(System.DefaultWorkingDirectory)/src/main/java/

All done, if you run the pipeline you could see the code coverage report generated and published.

Screen snippet of code coverage tab from the azure pipeline

--

--

Deepan Kumar Manivannan

“The future belongs to those who learn more skills and combine them in creative ways.”