Video in progress

Junior 🧪 Testing

What does test coverage measure, and does 100% coverage mean the code has no bugs?

Coverage shows how many lines or branches of code were executed while the tests ran. Even at 100% there can be bugs: the code may have been executed, but the result may never have been checked by any assert.

📖 Answer

JaCoCo is the usual tool. It gives two main numbers: line coverage (which lines ran) and branch coverage (did both sides of every if run).

Here's 100% coverage with a useless test:

@Test
void calculate() {
    calculator.calculate(order);   // no assert
}

The code ran, JaCoCo shows green, but the test passes even if the method returns the wrong amount.

The real value of coverage is the other way around: it shows which code isn't tested at all. At 40% that's a clear signal. The difference between 85% and 95% says little about quality.

In practice many teams set the threshold not for the whole project but for new code (for example, 80% of the changed lines in a PR). To check the quality of the tests themselves, there's mutation testing.