liveBook · Manning

Code coverage

As explained in Data-Oriented Programming in Java

Code coverage is a metric indicating which portions of source code are exercised by tests.

Code coverage measures which code tests execute, but a high percentage can conceal tests that assert little and contribute little to reliability. section 11.6.1

Use coverage cautiously:

  • Treat coverage as a proxy, not a complete measure of software quality. section 11.6.1
  • Prefer tests driven by data, domain properties, and meaningful assertions over tests written to exercise physical branches. section 11.6.1
  • Build confidence methodically rather than optimizing for an arbitrary coverage threshold. section 11.6.1
Listing 11.58 Asserting the mock responds with what you told it to mock
@Test
void literallyJustHereForCoverage() {
    Thing thing = mock(Thing.class);        #A 
    when(thing.getFoo()).thenReturn("Foo"); #A
    Service service = new Service(thing);   
    assertEquals("Foo", service.someMethod()); #B
}

A deliberately minimal test demonstrates how coverage can increase without testing meaningful behavior. section 11.6.1

How Data-Oriented Programming in Java covers it

How deep it goes
Contrasts branch-oriented coverage with data-focused testing, but stops short of defining a quantitative replacement.
Examples
Uses a mock-based test that returns "Foo" and asserts the result solely to increase executed coverage.
What it assumes
Assumes familiarity with tests, mocks, branches, assertions, and coverage thresholds.
What you can do after
Evaluate test suites by meaningful data and domain assertions rather than coverage percentage alone.
How it connects
Connects code coverage to testing, software reliability, test suites, data coverage, and domain properties.

Also explained in

Compare how 22 books explain code coverage →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.