liveBook · Manning

Arrange act assert pattern

As explained in Unit Testing Principles, Practices, and Patterns

A test-structuring pattern that prepares dependencies, invokes the behavior under test, and verifies the resulting outcome.

The Arrange-Act-Assert (AAA), or 3A, pattern gives unit tests a uniform structure: prepare the system under test and dependencies, invoke one behavior, then verify its outcome. Outcomes may be return values, state changes, or collaborator calls. section 3.1

Use the three phases deliberately:

  • Arrange the system under test (SUT) and its dependencies into the required state. section 3.1
  • Act by calling the behavior under test and capturing its result when applicable. section 3.1
  • Assert the expected result, final state, or interaction with a collaborator. section 3.1
  • Split tests containing multiple Arrange, Act, or Assert sections; they usually verify several behaviors at once. section 3.7
  • Treat a multi-line Act phase as an API warning: clients may need to perform coupled actions, risking invariant violations. section 3.7

Testing a calculator

Arrange two numbers and a Calculator named sut; act by calling Sum; assert that the result equals 30. Empty lines or phase comments make the boundaries visible without changing the test’s behavior.

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Explains each phase, contrasts Given-When-Then, and identifies multiple phases or multi-line Acts as maintainability warnings.
Examples
Uses calculator addition and store purchases to demonstrate setup, invocation, outcome checks, and collaborator effects.
What you can do after
Apply it to write readable, consistently structured unit tests and recognize tests that should be split or APIs that need encapsulation.
How it connects
Connects unit testing with the system under test, dependencies, Given-When-Then, test-driven development, and encapsulation.

Also explained in

Compare how 6 books explain arrange act assert pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.