State-based testing
State-based testing is a testing style that verifies behavior by examining the observable state of the system or its collaborators after an operation.
Unit Testing Principles, Practices, and Patterns
A unit-testing style that verifies the state of the system under test, a collaborator, or an out-of-process dependency after an operation.
- How it is used
- Verifies post-operation state when behavior is not captured solely by a return value.
- How deep it goes
- Defines the style, compares trade-offs, demonstrates assertions, and stops short of requiring total conversion to output-based testing.
- Examples
- Uses an Order collection, Article comments, and a filesystem-backed audit system to demonstrate state assertions and refactoring.
- What you can do after
- Apply it by asserting observable system, collaborator, or dependency state while managing brittleness and maintenance cost.
Read how Unit Testing Principles, Practices, and Patterns explains state-based testing →
Test Driven: Practical TDD and Acceptance TDD for Java Developers
State-based testing verifies behavior by comparing the state of the object under test and its collaborators with expected state.
- How it is used
- Verifies expected behavior by comparing the resulting state of the class under test and its collaborators with expected state.
- How deep it goes
- Defines state comparison and demonstrates simple tests with real objects and test doubles.
- Examples
- Uses a list that becomes nonempty after an addition and a PrintStream substitute that captures output in a buffer.
- What you can do after
- Evaluate behavior by comparing resulting object state with expected state.
The Art of Unit Testing 3e
A state-based test verifies behavior by examining the observable state produced or changed by the code under test.
- How it is used
- Verifies indirect outputs by checking changed state through another call or sibling property rather than the entry point’s return value.
- How deep it goes
- Defines indirect outputs, explains follow-up calls and sibling observables, and contrasts them with internal-call verification.
- Examples
- Uses follow-up calls, sibling functions or properties, and PasswordVerifier4’s protected findFailedRules() example.
- What you can do after
- Apply it by asserting observable state after invoking the entry point, while avoiding internal-call verification when possible.
Read how The Art of Unit Testing 3e explains state-based testing →
What to know first
- stubA stub is a minimal or generated stand-in for a software component or operation that exposes an interface while supplying placeholder, controlled, or delegated behavior.
- system under testA system under test is the component or system whose behavior is exercised and evaluated by a test.
Related concepts
- interaction testingcontrasts with · Interaction testing is a testing technique that verifies how a unit under test interacts with its collaborators, such as which calls it makes and with what parameters.
- output-based testingcontrasts with · Output-based testing evaluates software by comparing explicitly produced outputs with expected outputs rather than verifying internal interactions.
- software testingkind of · Software testing is the systematic evaluation of software under specified inputs or conditions to assess its behavior against expected results or requirements and identify defects.
- classical schoolcontrasts with · A unit-testing approach, also called the Detroit school or classical style, that isolates tests from one another while allowing the unit under test to use real collaborator objects.
Questions about state-based testing
What is state-based testing?
State-based testing is a testing style that verifies behavior by examining the observable state of the system or its collaborators after an operation.
Which Manning books explain state-based testing?
Unit Testing Principles, Practices, and Patterns, Test Driven: Practical TDD and Acceptance TDD for Java Developers and The Art of Unit Testing 3e.
What should I know before learning state-based testing?
Manning books that teach state-based testing usually introduce collaborator, stub and system under test first.
What is the difference between state-based testing and interaction testing?
State-based testing is a testing style that verifies behavior by examining the observable state of the system or its collaborators after an operation. Interaction testing is a testing technique that verifies how a unit under test interacts with its collaborators, such as which calls it makes and with what parameters.
What is state-based testing a kind of?
State-based testing is a kind of software testing. Software testing is the systematic evaluation of software under specified inputs or conditions to assess its behavior against expected results or requirements and identify defects.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


