A shared dependency is a dependency whose state is reused by multiple tests and gives those tests a way to affect one another’s outcomes. Typical examples include a static mutable field, singleton, or database. section 2.1 section 2.2
Classify dependencies by whether tests can share and mutate their state:
- A private dependency is not shared between tests; a mutable Store instance can remain intact when each test creates its own instance. section 2.1 section 2.2
- An out-of-process dependency runs outside the application process, but it is shared only when tests reuse the same mutable instance. section 2.1 section 2.2
- A read-only database or API can be out-of-process without being shared because tests cannot mutate it or alter one another’s execution. section 2.1 section 2.2
- A value object is private and immutable; Product.Shampoo and the number 5 are dependencies but not collaborators. section 2.2
Parallel database interference
One test creates a customer during arrangement while another deletes that customer before the first finishes. Running them in parallel can make the first test fail even when the production code is correct. Shared database state therefore requires isolation measures, such as replacing the dependency or running tests sequentially. section 2.1 section 2.4
The classical school isolates tests from one another, replacing only shared dependencies with test doubles. The London school isolates the class under test from all mutable dependencies, including private ones. Keeping private dependencies intact can preserve broader behavior-focused tests while avoiding shared-state interference. section 2.2 section 2.5
Figure 2.3. Isolating unit tests from each other entails isolating the class under test from shared dependencies only. Private dependencies can be kept intact.
Shows the isolation boundary: shared dependencies are replaced with test doubles, while private dependencies can remain intact. section 2.1
