An assertion is an executable check that a condition holds for a value, state, or computation. It turns an invariant into something a program can test rather than merely assume. section 7.5
Assertions become especially useful when the relevant states are made explicit:
- Enumerate every possible value of a simple or composite data type, including empty Optional states, then check the invariant for each state. section 7.5
- Combine exhaustive state sets to assert relationships between two or more states, such as the behavior of an operation over every pair of inputs. section 7.5
- Randomize attributes that a test does not use, expressing that its assertions should hold for all values of those attributes and reducing hidden coupling. section 11.4.1
- Treat data types as assertions: constructing an instance supplies proof that its required conditions have been satisfied, allowing typed boundaries around code. section 9.3.2
Checking every representable row
Generate every RawData combination, including optional values, and check the invariant for each row: for (RawData row : everyPossibleRow()) { assertTrue(someInvariant(row)); }. Nest the loops when the invariant relates two or more rows. section 7.5
Listing 7.24 Algebraic style assertions in Java
for (State state : allPossibleStates()) {
assertTrue(someInvariant(state));
}A compact Java pattern for asserting an invariant over every generated state. section 7.5

















