Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is the difference between line coverage and branch coverage in JaCoCo, and why is 100 percent coverage not proof of correctness?

In JaCoCo, line coverage counts how many source lines were executed by at least one test, while branch coverage counts how many of the possible paths out of each if, switch or boolean condition were actually taken. 100 percent coverage is not proof of correctness because coverage only records that code ran, not that any test checked the result: a test with no assertions covers code just as well as a good one. Coverage shows us which code is untested; it cannot tell us that the tested code is right.

Why this question is asked

Almost every Java project has a coverage report in its build, and many teams have a coverage threshold. Interviewers ask this question to check that we can read those reports correctly and that we do not mistake a high number for a well-tested system. In this answer, we will recap how JaCoCo collects coverage, compare line and branch coverage on a small method, show a test that reaches 100 percent while checking nothing, and finish with how coverage is used sensibly in CI and SonarQube.

1How JaCoCo Collects Coverage

JaCoCo is the standard coverage tool for Java. It does not need changes to our source code. Instead, it runs as a Java agent, a small library the JVM loads at start-up with the -javaagent option. As each class is loaded, the agent instruments its bytecode on the fly: it inserts tiny probes, essentially boolean flags, at points in the code. When a probe's code runs, its flag is set.

When the test JVM exits, the agent writes the flags to a file, usually target/jacoco.exec. A separate report step then reads that file together with the compiled classes and source code, and produces HTML, XML and CSV reports. With Maven, the jacoco-maven-plugin's prepare-agent goal adds the agent to the Surefire test run, the report goal writes the reports, and the check goal fails the build if a rule is not met.

Step throughFrom test run to coverage report
  1. prepare-agentadds -javaagent to the test JVM
  2. Classes loadthe agent inserts probes into bytecode
  3. Tests runexecuted probes set their flags
  4. JVM exitsflags written to jacoco.exec
  5. reportHTML for people, XML for CI and SonarQube
  6. check rules met?
    YesBuild continuesNoBuild fails

6 steps in order. Play them, or step through them one at a time.

JaCoCo measures execution through probes in the bytecode, not by reading our source.

2Line Coverage Versus Branch Coverage

Line coverage asks a simple question for each line of source: did any test execute at least part of it? Branch coverage asks a finer one: for each decision point, such as an if, a ternary, a switch or a && condition, how many of its possible outcomes did tests take? An if has two branches, true and false. A condition like a && b has four branch outcomes in JaCoCo's counting, because each part can be true or false.

Here is a method where the two numbers differ a lot.

Java
int discount(Customer c, int amount) {
int d = 0;
if (c.isVip() && amount > 100) d = 10;
return d;
}
@Test void vipWithLargeOrder() {
assertEquals(10, pricing.discount(vip, 150)); // passes
}
// JaCoCo for discount():
// lines: 4 of 4 covered (100%)
// branches: 2 of 4 covered (50%) -> the if line is shown yellow

Every line ran, so line coverage is 100 percent. But both conditions were only ever true. We never saw a non-VIP customer, and never a VIP with a small order. JaCoCo marks the if line in yellow, meaning partly covered, and the tooltip says 2 of 4 branches missed. Branch coverage is the more honest number for logic, because bugs usually hide in the path that was not taken.

3100 Percent Coverage That Checks Nothing

Now the important part. Look at this test.

Java
@Test void coversEverything() {
pricing.discount(vip, 150);
pricing.discount(vip, 50);
pricing.discount(regular, 150);
}
// JaCoCo for discount():
// lines: 4 of 4 (100%)
// branches: 4 of 4 (100%)
// assertions: none. If discount() returned 99 every time, this test would still pass.

The test takes every branch, so both metrics are now 100 percent. Yet it contains no assertion at all. We could change d = 10 to d = 1000, or delete the whole if, and the test would stay green. Coverage measured that the code was executed; it never measured that the result was checked.

That is the core reason 100 percent coverage is not proof of correctness. And even with good assertions, coverage cannot see missing code: if the requirement says VIP customers get 10 percent only on weekdays, and nobody wrote the weekday check, there is no line to be uncovered. Coverage can only report on the code that exists.

4What Coverage Is Good For

Coverage is still useful, as long as we read it the right way round. A low number is meaningful: an uncovered line has definitely not been tested, so the report is a map of where to look. A high number is not meaningful by itself: it tells us code ran, nothing more.

In CI, teams usually configure the JaCoCo check goal with a rule on the BRANCH or LINE counter, and publish jacoco.xml to SonarQube with the sonar.coverage.jacoco.xmlReportPaths property. SonarQube then shows coverage on new code in each pull request, which is the most useful view: it shows whether the change we are reviewing came with tests. To judge whether those tests check anything, we read them in review, and for critical code we can use mutation testing, which changes the code and checks whether a test fails.

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

This rule fails the build if less than 70 percent of branches across the module are covered. A moderate threshold like this catches code with no tests at all without pushing people to write tests just for the number.

5Follow-up questions

What other counters does JaCoCo report?

Besides lines and branches, it reports instructions (bytecode instructions executed, the most fine-grained counter), methods, classes and cyclomatic complexity. Instruction coverage is useful because it does not depend on how code is formatted across lines, while branch coverage is the best single number for decision logic.

Why can a line be partly covered?

A line can contain more than one branch, such as a condition with && or a ternary operator. If tests take only some of those branches, JaCoCo shows the line in yellow and reports how many branches were missed. It can also happen when an exception is thrown in the middle of a line, so later instructions on it never run.

Should we aim for 100 percent branch coverage?

Usually not. The last few percent tend to be defensive code, generated code or error paths that are expensive to trigger, and chasing them produces low-value tests. A moderate overall threshold plus good coverage of new and changed code, with reviewers checking that tests actually assert behaviour, gives more value.

6Conclusion

In this answer, we saw that JaCoCo instruments bytecode through a Java agent, records which probes ran, and turns that into reports for people, CI and SonarQube. Line coverage counts executed lines, while branch coverage counts the outcomes of each decision that tests actually took, so it is the stricter number for logic. Neither proves correctness: a test with no assertions reaches 100 percent, and missing requirements leave nothing to measure. Coverage tells us what is untested, not that what is tested is right.

What to say in the interviewAbout 80 seconds aloud

Open with

Line coverage counts the lines that at least one test executed. Branch coverage looks at each decision point, like an if or a condition with and or or, and counts how many of its possible outcomes the tests actually took.

Then, if they want more

So a method can show 100 percent line coverage and 50 percent branch coverage if every condition was only ever true; JaCoCo shows that line in yellow.

Under the hood JaCoCo runs as a Java agent, adds probes to the bytecode when classes load, writes the results to jacoco.exec, and the report goal turns that into HTML for us and XML for CI and SonarQube. The check goal can fail the build on a threshold.

The reason 100 percent coverage does not prove correctness is that coverage only measures execution. A test that calls every path and asserts nothing gets full coverage, and it would still pass if the method returned the wrong value. Coverage also cannot see missing code, like a requirement nobody implemented.

So I read it the other way round: low coverage reliably shows untested code, and high coverage tells me nothing about quality until I look at the assertions, or use mutation testing on critical code.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between line coverage and branch coverage in JaCoCo, and why is...": https://scrivio-interview-library.netlify.app/java/test-strategy/d51d09a6d1/