Lesson 4 of 5 · 11 min · Unit testing in Java
Test coverage and test quality
What you will learn
- What line, branch and method coverage mean
- Measure coverage with JaCoCo and read the report
- Set a sensible coverage target and where to apply it
- Why 100 percent coverage does not mean good tests
- Mutation testing, flaky tests and healthy test habits
In simple words
Coverage tells you which lines of your code were run while the tests ran. If a line never ran, no test could have caught a bug in it. That is useful. But a line that ran is not proven correct: a test that runs everything and checks nothing gives 100 percent coverage and no protection. Coverage is a flashlight that shows the dark corners, not a certificate that the room is clean.
Words to know
- Coverage: the share of your code that was executed by tests.
- Branch coverage: whether both outcomes of each
iforswitchwere taken. - Mutation testing: changing the code slightly to check that the tests notice.
- Flaky test: a test that sometimes passes and sometimes fails with no code change.
What coverage measures
| Metric | Counts |
|---|---|
| Line | lines executed |
| Branch | each outcome (true and false) of every decision |
| Method | methods called at least once |
| Class | classes loaded and used |
| Instruction | bytecode instructions executed (JaCoCo's basic unit) |
| Cyclomatic complexity | the number of independent paths, to see how many tests a method needs |
A method like this:
public String grade(int marks) {
if (marks >= 75) return "distinction";
if (marks >= 40) return "pass";
return "fail";
}
has three lines of logic. One test with marks = 80 executes 1 of 3 return paths: about 33 percent. Tests with 80 and 50 reach two of three; adding 10 reaches all three: 100 percent line and branch coverage. Branch coverage is stricter and more honest than line coverage, because a one-line x = a > b ? 1 : 2; counts as covered even when only one side ran.
JaCoCo with Maven
JaCoCo (Java Code Coverage) is the standard tool. Add the plugin to pom.xml:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.13</version> <!-- use the latest release -->
<executions>
<execution>
<goals><goal>prepare-agent</goal></goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals><goal>report</goal></goals>
</execution>
</executions>
</plugin>
Run ./mvnw verify. The HTML report is in target/site/jacoco/index.html: packages and classes with bars for instructions and branches. Click a class to see its source with green (covered), yellow (partly covered branches) and red (not covered) lines. IDEs can also run tests with coverage and colour the editor (IntelliJ: Run with Coverage). Look at the red and yellow lines: each one is a question, "which test would exercise this?".
To fail the build when coverage drops, add a check goal with rules:
<execution>
<id>check</id>
<goals><goal>check</goal></goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.70</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
Send reports to a tracking service (SonarQube/SonarCloud, Codecov, Coveralls) from CI to see trends and coverage of new code in each pull request.
What target makes sense
There is no magic number. Practical guidance:
- 60 to 80 percent overall is common and healthy for business applications. Aim higher (90 percent or more) for core logic (pricing, validation, state machines), where bugs are costly.
- Do not chase 100 percent. The last few percent are usually trivial code (getters,
main, configuration) and tests that exist only to colour lines green. - Apply a threshold to new and changed code ("at least 80 percent on this pull request"), which lifts quality without a huge rewrite.
- Exclude generated code, DTOs and configuration from the report (
<excludes>), so the number reflects real logic. - Track the trend: a falling number deserves a conversation.
Coverage is not quality
@Test
void coversEverythingAssertsNothing() {
calculator.discounted(1000, 10); // line covered, but nothing is checked
}
This test gives 100 percent coverage of discounted and would still pass if the formula were wrong. Goodhart's law applies (the KPI lesson): when coverage becomes a target, people write tests that satisfy the number. What matters is whether your tests fail when the code is wrong.
Coverage also cannot see: missing code (a requirement nobody implemented), wrong assertions, untested combinations of inputs, concurrency problems, the behaviour of dependencies, and whether the tests check the right things.
Mutation testing
A direct way to test your tests is to break the code on purpose. Mutation testing tools (PIT / Pitest for Java) make small changes, called mutants, such as turning >= into >, + into -, deleting a call or returning null, and run your tests against each. If a test fails, the mutant is killed (good). If all tests still pass, the mutant survived: your tests did not notice that change, which usually means a missing assertion or case.
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.19.0</version> <!-- check the latest version -->
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
</plugin>
Run ./mvnw org.pitest:pitest-maven:mutationCoverage and open target/pit-reports. The mutation score (killed mutants out of all) is a stronger quality signal than coverage. It is slower (it reruns tests many times), so run it on core packages or in a nightly job rather than on every commit.
Healthy test suites
- Fast: unit tests in seconds, the whole suite in a few minutes. Slow suites get skipped.
- Reliable: a flaky test (passes sometimes, fails sometimes) destroys trust. Common causes: shared state between tests, test order,
Thread.sleepand timing, the real clock and random numbers, unordered collections, external services, port clashes. Fix or delete flaky tests quickly; do not just re-run until green. - Independent: any test can run alone and in any order.
- Readable: a failing test should explain what broke from its name and message.
- Maintained like production code: refactor duplicated setup into builders and helpers (a
CustomerBuilderor test data factory), but keep each test understandable on its own. - Balanced (the pyramid): many unit tests, some integration tests with real databases (Testcontainers), few end-to-end tests.
- Run in CI on every pull request, with the build failing on a red test, and publish the report.
- Review tests in code review as carefully as code.
Where coverage helps most
Use it as a map, not a score: after writing tests for a feature, open the report, find red and yellow lines in the new code, and ask whether each is a case worth testing (an error branch, a boundary) or trivia. Add tests for the former. Look at coverage by class to find untested critical code, and combine with complexity: a complex method with low coverage is the biggest risk.
Common mistakes
- Treating a coverage percentage as a goal and writing assertion-free tests.
- Reading only line coverage and ignoring branches.
- Including generated code and DTOs, which distorts the number.
- Ignoring flaky tests, teaching the team to ignore red builds.
- No tests for error paths and boundaries, though the report looks fine.
- A slow suite that nobody runs locally.
Try it yourself
- Add JaCoCo to a project, run
./mvnw verifyand open the HTML report; find a yellow line and write the test that turns it green. - Write a test with no assertions, show that coverage rises, and explain why that is useless.
- Add a coverage
checkwith a modest limit and see the build fail when you delete a test. - Run PIT on one class and find a surviving mutant; add the assertion that kills it.
- Find or create a flaky test (shared static state, or a
sleep) and fix its cause.
Check yourself
What is the difference between line coverage and branch coverage?
Line coverage counts executed lines; branch coverage counts whether each outcome (true and false) of every decision was taken, which is stricter because one line can contain several branches.
Can 100 percent coverage be achieved with bad tests?
Yes. A test that executes code but asserts nothing covers lines without checking behaviour.
What is a surviving mutant?
A deliberate small change in the code that no test failed on, indicating a missing test or assertion.
What usually causes flaky tests?
Shared mutable state, test order dependencies, timing and sleeps, the real clock or randomness, and external systems.
Interview questions
What coverage percentage should a project aim for?
There is no universal figure; commonly 70 to 80 percent overall with higher coverage for critical logic, enforced on new code, and judged together with the quality of assertions, rather than aiming for 100 percent.
How do you know your tests are good, not just that they run the code?
They fail when the code is wrong: check with mutation testing, review assertions and edge cases, practise TDD or reproduce bugs with failing tests first, and track escaped defects.
How do you deal with a flaky test?
Reproduce it, find the source of non-determinism (shared state, time, ordering, external dependency), remove it by isolating state, injecting a clock or using fakes; quarantine temporarily if necessary but fix or delete it.
Summary
- Coverage shows which code ran in tests (line, branch, method); branch coverage is the honest measure.
- JaCoCo reports coverage in Maven; use it to find untested logic, set realistic targets and gate new code.
- Coverage does not equal quality; mutation testing (PIT) checks whether tests really catch changes.
- Keep suites fast, independent and non-flaky; run them in CI.
Going deeper
- JaCoCo documentation: the Maven plugin, counters and rules.
- PIT mutation testing: quickstart and the Maven plugin.
- Testcontainers: real databases in integration tests.
- JUnit User Guide: conditions, tags and test organisation.
See how much of this you can explain
A free mock interview asks you new questions on Unit testing in Java with JUnit and Mockito for Java Full Stack Developer and scores every answer, with what to fix.