liveBook · Manning

Dependency

As explained in Unit Testing Principles, Practices, and Patterns

A dependency is a component, object, service, or value that code under test relies on.

A dependency is a component, object, service, or value that code under test relies on. Testing strategy depends on whether it is mutable, shared, or out-of-process. section 2.2

Use these distinctions when choosing real dependencies or test doubles:

  • Keep immutable values and value objects real; substitute mutable dependencies when isolation requires it. section 2.2
  • The London school isolates the system under test and replaces mutable dependencies; the classical school isolates tests and replaces shared dependencies. section 2.2
  • Prefer explicit injection over ambient context, and pass time as a value when practical. section 11.6

Purchase and time

A purchase test keeps Product.Shampoo and 5 real while mocking the mutable Store. For time, inject a clock service at the operation boundary, then pass its DateTime value into domain logic. section 2.2 section 11.6

Table 2.1. The differences between the London and classical schools of unit testing, summed up by the approach to isolation, the size of a unit, and the use of test doubles (view table figure)
 

Isolation of

A unit is

Uses test doubles for

London school Units A class All but immutable dependencies
Classical school Unit tests A class or a set of classes Shared dependencies

Compare isolation targets, unit sizes, and test-double boundaries across the two testing schools. section 2.2

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Classifies dependencies by mutability and sharing, then contrasts substitution with explicit injection.
Examples
Uses a mutable Store, immutable Product values, a mocked purchase collaborator, and an injected date-time service.
What it assumes
Assumes familiarity with unit-test isolation and collaborators; distinguishes immutable values from mutable state.
What you can do after
Apply it via explicit dependency injection and selective test doubles while preserving immutable values as real collaborators.
How it connects
Connects dependencies to unit testing, isolation, test doubles, value objects, dependency injection, and integration testing.

Also explained in

Compare how 27 books explain dependency →

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.