A correctness guarantee starts where coverage stops. Executing a line or branch only proves that a particular implementation path ran; it does not show that the behavior is the one the domain requires. The useful question is not whether tests agree with the code, but what laws the code must satisfy for every relevant state. section 7.1 section 11.3.4
Make the intended meaning explicit as equations, invariants, or ordering rules over data. Once those statements are precise, tests can check an implementation against them rather than merely replaying its control flow. A complete algebra need not be elaborate: even one operation and the laws it must obey can define what correct behavior means. section 7.1 section 7.12
Common forms of correctness guarantees include:
- Associativity: combining three values gives the same result regardless of grouping, enabling work to be decoupled from a fixed sequential grouping. section 7.12
- Commutativity: swapping two arguments does not change the result when argument order has no business meaning. section 7.12
- Ordering laws: reflexivity, antisymmetry, and transitivity define a valid comparison relation; monotonicity checks that an operation preserves that order. section 7.12
- Domain invariants: a set of business rules can require disjoint inputs and outputs so that no two rules duplicate, overlap, or conflict. section 8.1.1
Non-overlapping account rules
Model each rule as a function from an Account to an optional SalesOrgId. Correctness requires that distinct rules neither act on the same accounts nor generate the same organization ID. Helpers that compute a rule’s domain and image turn those global requirements into a test: compare every pair of known rules and assert that both intersections are empty. One property-based test can then expose a duplicated rule, an overlapping input, or conflicting output. section 8.1.1 section 8.1.1
This approach changes implementation choices as well as tests. If a business lifecycle is really an ordering, encode states so the ordering expresses permitted transitions. An empty state can be below every concrete state, terminal states above all nonterminal states, and a state excluded from transitioning to itself. The resulting law is clearer than a dense database condition built from AND, OR, and NOT expressions. section 12.3 section 12.3
Listing 12.9 Verifying our transitions
@Test
void encodingStateTransitionsViaOrdering() {
TaskId taskId = new TaskId(String.valueOf(random.nextInt()));
List<Optional<Job>> states = List.of(
Optional.empty(), #A
Optional.of(new Started(taskId)), #A
Optional.of(new Failed(taskId)), #A
Optional.of(new Completed(taskId)) #A
);
for (var a : states) { #B
for (var b : states) { #B
System.out.println(format("%-10s can transition to %-10s %s",
a,
b,
order(a) < order(b))
);
}
}
}
// [out]
// (simplified to fit on the page)
//
// (Empty) can transition to (Empty) false
// (Empty) can transition to Started true #C
// (Empty) can transition to Failed true #C
// (Empty) can transition to Completed true #C
// Started can transition to (Empty) false
// Started can transition to Started false #D
// Started can transition to Failed true #D
// Started can transition to Completed true #D
// Failed can transition to Failed false #D
// Completed can transition to Completed false #D
// Failed can transition to (Empty) false #E
// Failed can transition to Started false #E
// Failed can transition to Completed false #E
// Completed can transition to (Empty) false #E
// Completed can transition to Started false #E
// Completed can transition to Failed false #EThe transition table makes the ordering concrete by enumerating every pair of lifecycle states and showing which moves are allowed. section 12.3
A guarantee is only as strong as the boundary where it is checked. Rules involving persistence, concurrency, or state transitions must be enforced by the dependency that owns the shared state; application-only checks can lose races. Integration tests then verify that the real dependency accepts precisely the valid transitions and rejects the invalid ones. Randomized, concurrent exploration can reach failures that isolated unit tests cannot. section 12.3 section 12.7
Types also contribute to correctness when they make meaningful states explicit. Records, sealed state variants, and dedicated domain types replace vague fields, flags, and null-driven ambiguity with values that describe what is known about the system. This is a tradeoff: extra types add boilerplate, but can improve safety, correctness, and understandability when their semantics justify them. section 11.3.4 section 3.4
















































