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

Minimise gap between defect insertion and defect detection

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

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>
<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/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Deepan Kumar Manivannan

Deepan Kumar Manivannan

17 Followers

“The future belongs to those who learn more skills and combine them in creative ways.” ― Robert Greene, Mastery