# Specification Coverage vs. Line Coverage

> Source: <https://martinelli.ch/specification-coverage-vs-line-coverage/>
> Published: 2026-11-26 09:27:45+00:00

**Recommendation:** Tag business rules in code and tests with use-case IDs, then run a specification-coverage check so every step, alternative flow and rule has both implementing code and a proving test, not just executed lines.
More in the playbook "Evals and testing for LLM apps": <https://wpnews.pro/playbooks/evals-testing>

The build is green, and SonarCloud reports 98.7% code coverage, with 99.8% line coverage. Only one line in the whole application is not executed by a test. That still tells you nothing about whether the application does what the business asked for.

With AI coding agents, this gap gets bigger. The same agent writes the code and the tests in one run. The tests pass, the coverage number goes up, and nobody has checked whether the result does what the business asked for.

In this post, I show a different kind of coverage: specification coverage. I use the PetClinic example of the AI Unified Process and run the coverage check on a real use case.

## Two Different Questions

Line coverage and specification coverage answer different questions. You need both.

|  | Line coverage | Specification coverage | 
|---|---|---|
| Question | Which code ran during the tests? | Is every behavior we agreed on built and tested? | 
| Starting point | The code | The specification | 
| Unit | Line, branch, method | Step, alternative flow, business rule, pre- and postcondition | 
| Tool | JaCoCo, Istanbul, coverage.py | The `/use-case-coverage` skill of the AI Unified Process | 
| Finds | Code no test executes | Behavior with no code, behavior with no test, code with no specification | 
| Blind spot | Missing code and weak assertions | Code quality inside a covered unit | 

Line coverage starts from the code. If a business rule was never implemented, there is no line to miss, so the report can be at 100% and the rule is still missing. And a line counts as covered as soon as it runs, even when no assertion checks the result.

Specification coverage starts from the specification. It goes through every unit of the use case and asks two things: where is the code that implements it, and where is the test that proves it?

## Coverage Units in a Use Case

Specification coverage only works if the specification has parts you can count. A system use case in the AI Unified Process has exactly that. Every part has an ID or a number:

- **Preconditions** (Pre)
- **Main success scenario steps** (Step 1 to Step n)
- **Alternative flows** (A1, A2, …)
- **Business rules** (BR-001, BR-002, …)
- **Success and failure postconditions** (Post-S-1, Post-F-1, …)
- **Linked non-functional requirements** (NFR-003), where they apply to this use case

Each of these is one coverage unit. A user story with a few acceptance criteria in free text is much harder to audit this way. This is one more reason why I prefer use cases as the specification for business applications.

## Markers Make It Traceable

To find the code and the tests for a unit, the coverage check uses markers. The project defines them in `docs/architecture/stack.md`, so every layer skill writes them the same way.

In the code, a comment directly above the line that enforces a business rule names the use case and the rule. This is the visit form of PetClinic:

```
// UC-009 BR-004: every visit has a date; any calendar date is accepted, so no min or max.
binder.forField(date)
        .asRequired(REQUIRED)
        .bind("visitDate");
binder.forField(description)
        .withDefaultValidator(false)
        // UC-009 BR-007: surrounding spaces are removed before validation and recording.
        .withConverter(String::trim, s -> s)
        // UC-009 BR-001: every visit has a non-blank description; only spaces is blank.
        .asRequired(REQUIRED)
        // UC-009 BR-005: at most 255 characters, rejected on the field like any other validation error.
        .withValidator(new StringLengthValidator(
                "At most " + DESCRIPTION_LENGTH + " characters", 0, DESCRIPTION_LENGTH))
        .bind("description");
```

In the tests, a `@UseCase` annotation says which use case, which scenario, and which business rules a test method checks:

```
@Test
@UseCase(id = "UC-009", businessRules = {"BR-005", "BR-006"}, scenario = "A5: Description Too Long")
void overLongDescriptionIsAFieldErrorAndKeepsEveryValue() {
    ...
}
```

The annotation is a plain Java annotation in the test sources. It also has a `requirements` attribute for NFRs. You will see in a moment why that matters.

The markers are not the proof. They are the index. The coverage check still reads the code and the test behind each marker and decides whether they really do what the unit says.

## Example: UC-009 Book Visit for Pet

The [PetClinic example](https://github.com/AI-Unified-Process/petclinic) is built with Spring Boot, Vaadin Flow, jOOQ and Flyway. UC-009 lets a clinic user record a visit for a pet. It has 6 steps, 6 alternative flows, 7 business rules and 3 postconditions.

I ran the check like this:

```
/use-case-coverage UC-009 html
```

The skill hands the work to a read-only sub-agent. That agent gets only the use case ID and the mode, nothing else. It does not get a list of files that I think implement the use case. It has to find its own evidence. Otherwise the audit quickly becomes a rubber stamp.

The result: implementation 23/23 and tests 23/23, with one unit marked n/a because the use case has no preconditions. Here is an excerpt of the matrix:

| Unit | Description | Implementation | Test | Verdict | 
|---|---|---|---|---|
| Step 4 | Trim, then validate date required, description not blank and within max length | VisitForm:54-66 | UC009Test:122-130, 262-273, 283-293, 303-311 | Covered | 
| A2 | Pet does not exist or belongs to another owner: error view | BookVisitForPetView:97-102 | UC009Test:157-168, 172-183 | Covered | 
| BR-003 | Owner/pet consistency | BookVisitForPetView:99-102 | UC009Test:157-168 (rejected); 44-74 (allowed) | Covered | 
| BR-005 | At most 255 characters, rejected on the field | VisitForm:37, 63-65 | UC009Test:261-273 (256 rejected); 282-293 (255 accepted) | Covered | 
| Post-F-1 | No visit is recorded | BookVisitForPetView:118 | UC009Test:114-117, 134-136, 150-153, 315-317 | Covered | 
| NFR-003 | A pet’s visits by ascending date | VisitRepository:30-31 | UC009Test:53, 256 | Covered (marker missing) | 

Two details are worth pointing out. First, every verdict names a file and a line. The rule of the agent is simple: no file and line, no Covered. Second, for business rules, the check looks for both sides. BR-005 has a test where 256 characters are rejected and one where 255 characters are accepted. A test of only one side would be reported as Partial.

With `html`, the skill also writes `docs/coverage/UC-009.html`. It shows the score as tiles, the matrix with a badge per verdict, and a filter for the open rows.

### The Gap

The use case links NFR-003 (a pet’s visits are shown by ascending date). The behavior is implemented and tested. But no test names NFR-003 in its `@UseCase` annotation, while the UC-005 tests already do it. So the traceability from the requirement to the test is broken. This is not missing behavior, but it would hurt in an audit or when NFR-003 changes. The check proposes `/use-case-test UC-009` to close it.

### The Drift

The more interesting finding goes in the other direction. The visit form shows a text when the pet has no visits yet:

```
if (visits.isEmpty()) {
    previousVisits.add(new Paragraph("(none)"));
}
```

UC-009 does not mention this text. And UC-005 specifies “(no visits yet)” for the same situation on the Owner Details view. So the code does something that is not in the specification, and it is not consistent with the rest of the application. And note: this line is not untested in the line coverage sense. The end-to-end test TC002 books a visit for a brand-new pet, so the line runs. It just has no assertion on the text.

The check does not fix this. It cannot know which side is right. Either the specification is missing the text (`/use-case-spec UC-009`), or the code should be changed (`/use-case-implement UC-009`). This is a decision for a person, ideally together with the business.

## What Line Coverage Would Have Told Us

PetClinic is analyzed on [SonarCloud](https://sonarcloud.io/project/overview?id=AI-Unified-Process_petclinic) with the merged JaCoCo report of the Browserless and the Playwright tests. The analysis from 18 November 2026 shows:

- **Coverage (lines and branches combined):** 98.7%
- **Line coverage:** 99.8% (1 of 634 lines not covered)
- **Branch coverage:** 84.0% (8 of 50 conditions not covered)
- **BookVisitForPetView, VisitForm, VisitRepository:** 100% line coverage, and 100% branch coverage where there are branches

By this measure, UC-009 is perfect. Now the same three findings through the eyes of line coverage:

| Finding | Line coverage | Specification coverage | 
|---|---|---|
| “(none)” branch | Green. TC002 executes the line. | Drift: behavior that is in no specification and contradicts UC-005. | 
| NFR-003 not named in a test | Invisible. The lines run, so they are green. | Gap in traceability from requirement to test. | 
| BR-005 accepted case (255 characters) | Not needed. The 256 test already runs the same lines. | Required. A rule is only covered when both the allowed and the rejected case are tested. | 

The first row is the important one. The line is covered, because a test happens to pass through it. But no test says what it should show, and no specification says it either. A coverage number counts executed lines, not checked behavior. With AI agents, this gets worse, because “increase the coverage” is an easy task to give an agent, and an agent can reach 100% without a single meaningful assertion.

So line coverage still has its place. It shows dead code and code paths that no test reaches. But it cannot tell you whether the system does what was agreed. Only a comparison with the specification can do that.

## Coverage and the Use Case Status

Every use case in the AI Unified Process has a status: Draft, Reviewed, Approved, Implemented, Tested, Done, or Obsolete. The coverage check connects these statuses to evidence.

- **Implemented** means every unit has code behind it.
- **Tested** means every unit also has a test, and the test suite passes.
- **Done** means the business accepted it. This is not visible in code, so it comes from the acceptance protocol, not from the coverage check.

For UC-009, the spec says Done. The check said that the code and tests support Tested at most, and that Done must come from acceptance. It did not change the status line. It only suggests a status. The decision stays with the team.

## Limits

Specification coverage is not magic, and I want to be clear about what it cannot do.

- **It does not run the tests.** The agent reads code and tests. It can say that a test checks BR-005, but not that the test is green. Before you accept a Tested suggestion, run the build.
- **It is a judgment, not a measurement.** An AI agent decides whether a test really checks a rule. Two runs can differ on a borderline case. That is why every verdict must name the file and line, so a person can check it in a minute.
- **It depends on the markers.** Without markers, the agent has to search much harder, and the reverse check for drift gets weaker. In this run, the agent itself reported that it could not sweep the whole tree for stray UC-009 strings.
- **It is only as good as the specification.** If a business rule is missing in the use case, the check cannot report it as missing in the code. Reviews of the specification with the business stay essential.

## Conclusion

Line coverage tells you which code ran. Specification coverage tells you whether the system does what you agreed on. When AI agents write both the code and the tests, the second question becomes the important one.

In PetClinic, a use case that looked complete still had a broken link from a requirement to its tests, and a small piece of behavior that nobody had specified. Line coverage showed 100% for every class involved and found neither.

You can try it yourself. Clone the [PetClinic example](https://github.com/AI-Unified-Process/petclinic), install the AI Unified Process plugins in Claude Code, and run:

```
/plugin marketplace add ai-unified-process/marketplace
/plugin install aiup-core
/use-case-coverage UC-009 html
```

More about the AI Unified Process is on [unifiedprocess.ai](https://unifiedprocess.ai).
