liveBook · Manning

Assertion

As explained in Data-Oriented Programming in Java

An assertion is an executable check that a specified condition holds for a value, state, or computation.

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

How Data-Oriented Programming in Java covers it

How deep it goes
Works through exhaustive state generation, multi-state combinations, and randomized test data, but leaves randomization problems for later treatment.
Examples
Demonstrates integer arithmetic, RawData rows, pairwise operations, ValidCart boundaries, and randomized Account fixtures.
What it assumes
Assumes Java and domain modeling familiarity while developing state-space reasoning, invariants, and data-oriented testing practices.
What you can do after
Diagnose/debug with it by checking invariants across explicit, exhaustive, relational, or randomized test states.
How it connects
Connects assertions with invariants, data types, state spaces, randomization, and the Curry-Howard Correspondence.

Also explained in

Compare how 18 books explain assertion →

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.