Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 41 ยท Testing and Quality

Code Quality: Coverage and Static Checks

Add quality gates to a Maven build: JaCoCo coverage thresholds, Checkstyle, SpotBugs, the Enforcer plugin and formatter plugins, and fail the build on violations.

Passing tests tell you that the behavior you thought of works. They do not tell you how much of the code the tests touched, whether the style is consistent, whether there are classic bug patterns, or whether the build is even running on the right Java version. Maven can check all of that during the build and fail it when a rule is broken, so problems are caught before review instead of after release.

This lesson tours the most common quality plugins: JaCoCo for coverage, Checkstyle for style, SpotBugs for bug patterns, the Enforcer plugin for build rules and a formatter plugin for layout. These plugins download from Maven Central and write to target/, so the configuration is read-only here. The runnable parts are Java stand-ins: a coverage calculator with a threshold check and a small rule checker in the spirit of the Enforcer. They show the logic of a quality gate, not the real tools. Plugins and goals are explained in Plugins and Goals; tests in Running Tests with Surefire.

The idea of a quality gate

A gate is a check that can say no. A report nobody reads changes nothing, but a build that turns red when coverage drops or a banned dependency appears gets attention at the right moment, on the author's own machine or in the pull request. Most quality plugins have a "report" goal, which only writes files, and a "check" goal, which fails the build. The check goal is what makes it a gate.

Four kinds of checks

Coverage How much code do tests run? JaCoCo.
Style Naming and layout rules. Checkstyle.
Bug patterns Likely mistakes in bytecode. SpotBugs.
Build rules Java version, banned libraries. Enforcer.
1 / 2

Coverage with JaCoCo

JaCoCo measures which lines and branches your tests execute. It works by attaching a Java agent to the test JVM, which records execution into a file. A separate goal turns that file into a report.

XML
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.14</version>
  <executions>
    <execution>
      <goals><goal>prepare-agent</goal></goals>
    </execution>
    <execution>
      <id>report</id>
      <phase>verify</phase>
      <goals><goal>report</goal></goals>
    </execution>
    <execution>
      <id>check</id>
      <phase>verify</phase>
      <goals><goal>check</goal></goals>
      <configuration>
        <rules>
          <rule>
            <element>BUNDLE</element>
            <limits>
              <limit>
                <counter>LINE</counter>
                <value>COVEREDRATIO</value>
                <minimum>0.80</minimum>
              </limit>
            </limits>
          </rule>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>

Use a JaCoCo release that supports your Java version, because the agent reads class files and needs to understand their format. The three executions do different jobs:

  • prepare-agent runs before the tests (it binds to initialize by default) and sets the argument that attaches the agent to Surefire's JVM.
  • report creates target/site/jacoco/index.html, a browsable report with colored lines.
  • check compares the numbers with your rules and fails the build when one is not met.

A rule has an element (BUNDLE is the whole module, PACKAGE or CLASS are finer), a counter (LINE, BRANCH, INSTRUCTION, METHOD) and a value such as COVEREDRATIO with a minimum between 0 and 1. A line is covered if at least one test ran it; a branch is covered when both outcomes of an if were taken.

Coverage tells you which code ran, not whether the assertions are good. A test with no assertions can give 100% coverage. Use a threshold to stop the number sliding down, not as a measure of test quality, and be careful with very high targets, which make people write tests for the numbers.

Style with Checkstyle

Checkstyle reads your source files and applies a rule set: naming, line length, missing Javadoc, unused imports. It ships with two well-known rule sets, sun_checks.xml and google_checks.xml, and you can provide your own XML file.

XML
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-checkstyle-plugin</artifactId>
  <version>3.6.0</version>
  <configuration>
    <configLocation>google_checks.xml</configLocation>
    <violationSeverity>warning</violationSeverity>
    <failOnViolation>true</failOnViolation>
  </configuration>
  <executions>
    <execution>
      <phase>validate</phase>
      <goals><goal>check</goal></goals>
    </execution>
  </executions>
</plugin>

It runs in validate, the very first phase, so a style problem fails the build in seconds, before anything compiles. Start with a small rule set the team agrees on. A set of hundreds of rules nobody chose produces noise, and noise teaches people to ignore the build.

Bug patterns with SpotBugs

SpotBugs analyzes compiled bytecode and looks for patterns that are usually mistakes: a possible NullPointerException, an unclosed resource, comparing strings with ==, ignoring a method's return value. Because it needs class files, it must run after compile:

XML
<plugin>
  <groupId>com.github.spotbugs</groupId>
  <artifactId>spotbugs-maven-plugin</artifactId>
  <version>4.9.3.0</version>
  <configuration>
    <effort>Max</effort>
    <threshold>Medium</threshold>
  </configuration>
  <executions>
    <execution>
      <goals><goal>check</goal></goals>
    </execution>
  </executions>
</plugin>

threshold decides how confident SpotBugs must be before it reports: Low, Medium or High. Some findings are false positives. Rather than turning the whole tool off, you list exclusions in a filter file so that each one is a visible decision.

Build rules with the Enforcer

The Enforcer plugin checks facts about the build itself, not your code. Its enforce goal runs rules such as:

XML
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>3.5.0</version>
  <executions>
    <execution>
      <id>enforce</id>
      <goals><goal>enforce</goal></goals>
      <configuration>
        <rules>
          <requireMavenVersion><version>[3.9.0,)</version></requireMavenVersion>
          <requireJavaVersion><version>[25,)</version></requireJavaVersion>
          <dependencyConvergence/>
          <banDuplicatePomDependencyVersions/>
          <bannedDependencies>
            <excludes>
              <exclude>commons-logging:commons-logging</exclude>
            </excludes>
          </bannedDependencies>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>

The rules say: use at least Maven 3.9, use Java 25 or newer, fail if two paths in the dependency tree ask for different versions of the same library (dependencyConvergence, covered in Managing Versions), fail if a dependency is declared twice, and forbid a specific library. The enforce goal binds to validate by default, so this too fails early. A clear message such as "this project needs Java 25" saves a new teammate an hour of puzzling over a cryptic compiler error.

Formatters

Style arguments are a waste of review time. A formatter plugin removes them by rewriting files to one layout. The widely used Spotless plugin has two goals: apply rewrites the files, and check fails the build if any file is not already formatted. A common setup is to run mvn spotless:apply locally (or in an editor hook) and spotless:check in CI, so the pipeline never changes code, it only verifies it. Other formatter plugins work the same way.

Putting the gates in order

Cheap checks should fail first. A sensible order, which the default lifecycle already follows, is:

  1. validate: Enforcer rules, Checkstyle or a formatter check.
  2. compile, test-compile: the compiler is itself a check.
  3. test: unit tests with the JaCoCo agent attached.
  4. verify: coverage check, SpotBugs, then integration tests (Integration Tests with Failsafe).

A failure in validate costs seconds; a failure in verify can cost minutes. Put each check in the earliest phase where it can work.

Stand-in: a coverage calculator with a gate

The program below takes a list of classes with their total and covered line counts. It prints a table, computes the overall ratio the way a BUNDLE rule does (total covered lines divided by total lines, not an average of percentages), and applies a minimum like the check goal. It also shows a per-class rule, where one weak class fails even when the overall number looks fine.

Java Coverage ratio and the check goal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The bundle ratio is 197 of 300 lines, about 65.7%, so the bundle rule fails. Notice how LegacyImporter drags the total down even though the other three classes are above 80%. Real JaCoCo reports per-class numbers in its HTML report for exactly this reason: you want to see where the gap is.

Stand-in: a tiny Enforcer

The Enforcer's rules are simple facts compared with expectations. This second program models three of them: a minimum Java version, a banned dependency and duplicate dependency versions. It is a model of the logic, with simplified version comparison (whole numbers only).

Java A mini Enforcer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

All three problems are reported in one run, which is how the real plugin works too (it lists every failed rule unless you set failFast). That is helpful: you fix them all at once instead of one failed build at a time.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What makes a quality plugin fail the build instead of only producing a report?

    Report goals write files. Check goals compare results with rules and fail the build when a rule is broken.
  2. 2. A project has three classes covered 90% and one class covered 0%, all the same size. What does a BUNDLE line-ratio rule use?

    BUNDLE aggregates lines across the module. Here that is 67.5%. A CLASS-level rule would catch the 0% class on its own.
  3. 3. Which plugin analyzes compiled bytecode for bug patterns such as a possible null dereference?

    SpotBugs works on class files, so it runs after compilation. Checkstyle reads source files.
  4. 4. Why does it help to run Enforcer and Checkstyle in the validate phase?

    Put each check in the earliest phase where it can work, so the cheapest failures come first.
  5. 5. A test with no assertions executes every line of a class. What does JaCoCo report?

    Coverage measures execution, not verification. High coverage with weak assertions is possible, so do not treat the number as proof of quality.

Summary

  • A quality gate is a check that can fail the build; use check goals, not only report goals.
  • JaCoCo records coverage with an agent (prepare-agent), builds a report (report) and enforces limits (check) by element, counter and minimum.
  • Checkstyle checks source style, SpotBugs checks bytecode for bug patterns, the Enforcer checks build facts such as Java version and banned dependencies.
  • A formatter plugin such as Spotless removes style debates: apply locally, check in CI.
  • Put cheap checks in early phases so failures are fast, and start with small agreed rule sets.
  • Coverage shows what ran, not whether the tests are good.

Next: Packaging: JAR, WAR and Executable Jars