Skip to main content
Unit testing is a software development practice where individual units—such as functions, methods, or classes—are tested in isolation to ensure they behave as expected. By adopting unit tests early, teams can:

Running Unit Tests with Maven

In a Maven-based project, execute all unit tests with:
Maven automatically scans src/test/java (and any additional test directories you’ve configured), runs tests, and fails the build if any test does not pass:
By default, Maven looks for test classes matching **/*Test.java under src/test/java. You can customize this in the <build><plugins> section of your pom.xml.
A failing test will stop the Maven build. Ensure you fix or temporarily disable flaky tests to keep CI pipelines green.

Measuring Code Coverage with JaCoCo

JaCoCo is the de facto tool for generating Java code coverage reports. When integrated into Maven, it produces detailed HTML reports showing line and branch coverage.

1. Add the JaCoCo Plugin

Include the following snippet in your pom.xml under <build><plugins>:
You can configure coverage thresholds to enforce minimum percentages. If thresholds are not met, the build will fail.

2. Generate the Report

Run your tests and generate the coverage report:
After completion, open:
to view:
  • Overall Coverage: Total percentage of covered code.
  • Per-Package/Class Breakdown: Drill down into modules.
  • Covered vs. Uncovered Lines: Visual indicators in source code.
Thank you for reading!

Watch Video