DevSecOps - Kubernetes DevOps & Security
DevOps Pipeline
Demo Unit Test and JaCoCo
In this hands-on guide, you'll learn how to configure a Jenkins Pipeline to run unit tests on a Spring Boot application and generate code coverage reports using the JaCoCo plugin. By visualizing test results and coverage metrics directly in Jenkins, you can maintain quality gates and improve code reliability.
Prerequisites
- A running Jenkins server (version 2.x or later)
- Jenkins plugins: Pipeline, JUnit, JaCoCo (v3.2.0+)
- A Spring Boot project with Maven build (
pom.xml
) - Git repository with sample tests under
src/test/java
1. Review the Unit Test Cases
Navigate to src/test/java
and inspect the NumericApplicationTests
class. It uses MockMvc to validate HTTP endpoints:
Test Method | Endpoint | Expected Response |
---|---|---|
smallerThanOrEqualToFiftyMessage | GET /compare/49 | "Smaller than or equal to 50" |
greaterThanFiftyMessage | GET /compare/51 | "Greater than 50" |
welcomeMessage | GET / | HTTP 200 OK |
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class NumericApplicationTests {
@Autowired
private MockMvc mockMvc;
@Test
public void smallerThanOrEqualToFiftyMessage() throws Exception {
this.mockMvc.perform(get("/compare/49"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string("Smaller than or equal to 50"));
}
@Test
public void greaterThanFiftyMessage() throws Exception {
this.mockMvc.perform(get("/compare/51"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string("Greater than 50"));
}
@Test
public void welcomeMessage() throws Exception {
this.mockMvc.perform(get("/"))
.andDo(print())
.andExpect(status().isOk());
}
}
2. Update Your Jenkinsfile to Run Unit Tests
Open the root Jenkinsfile
. If you only build the artifact, insert a dedicated Unit Tests stage to execute mvn test
:
pipeline {
agent any
stages {
stage('Build - Maven') {
steps {
sh 'mvn clean package -DskipTests=true'
archive 'target/*.jar'
}
}
stage('Unit Tests') {
steps {
sh 'mvn test'
}
}
}
}
Commit and push your changes to trigger a new build in Jenkins. Once the pipeline runs, you’ll see a stage summary:
The console logs will confirm all tests passed:
[INFO] Results:
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
3. Configure JaCoCo in pom.xml
To collect coverage data, add the JaCoCo Maven plugin under the <build><plugins>
section:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Note
The prepare-agent
goal instruments bytecode before tests, while the report
goal generates HTML/XML coverage reports in target/site/jacoco
.
4. Publish JUnit and JaCoCo Reports in Jenkins
Ensure the Jenkins JaCoCo plugin (v3.2.0+) is installed under Manage Jenkins → Installed Plugins:
Then, refine the Unit Tests stage to archive JUnit results and publish coverage:
pipeline {
agent any
stages {
stage('Build - Maven') {
steps {
sh 'mvn clean package -DskipTests=true'
archive 'target/*.jar'
}
}
stage('Unit Tests & Coverage') {
steps {
sh 'mvn test'
}
post {
always {
// Publish JUnit results
junit 'target/surefire-reports/*.xml'
// Publish JaCoCo coverage
jacoco execPattern: 'target/jacoco.exec'
}
}
}
}
}
Commit and push again. After the build, Jenkins will display JaCoCo metrics in the console:
[jacoco plugin] Collecting JaCoCo coverage data...
[jacoco plugin] Overall coverage: class: 100.0, method: 71.43, line: 61.11, branch: 100.0, instruction: 40.91, complexity: 75.0
Navigate to the build’s Code Coverage Report for a breakdown by package and file:
Warning
Any untested code (e.g., methods without assertions) will show 0% coverage. Add test cases to improve your coverage metrics.
Next Steps
With your unit tests and coverage reports in place, you can now build a Docker image for this Spring Boot application and push it to Docker Hub.
Links and References
Watch Video
Watch video content
Practice Lab
Practice lab