liveBook · Manning

Private dependency

As explained in Unit Testing Principles, Practices, and Patterns

A dependency that is not shared between tests and therefore cannot transmit state between their executions.

A private dependency is not shared between tests, so its state cannot transmit interference between their executions. A test may create and reuse its own instance without making that dependency shared. section 2.1

Classify private dependencies by whether their state can change:

  • An immutable private dependency is a value object; equivalent instances can be used interchangeably. section 2.2
  • A mutable private dependency can change during a test but remains private when tests do not reuse the same instance. section 2.2 section 2.1

Private dependencies do not require replacement merely to isolate tests. The classical approach keeps them intact, while the London approach replaces mutable private dependencies as well as shared ones; immutable dependencies remain intact. section 2.2

Store and Product

A Store created inside each purchase test is a mutable private dependency. Product.Shampoo is an immutable private dependency, or value object. A database reused by tests is shared instead, because its state can affect other tests. section 2.2 section 2.1

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines private dependencies, distinguishes mutable and immutable forms, and contrasts classical and London treatment.
Examples
Uses Store instances, Product.Shampoo, databases, static fields, and singleton instances to classify dependencies.
What it assumes
Distinguishes dependencies from collaborators and understands that shared mutable state can affect test outcomes.
What you can do after
Evaluate whether a dependency can remain intact or needs replacement to prevent tests from affecting one another.
How it connects
Connects private dependencies with shared dependencies, value objects, collaborators, test doubles, and unit testing.

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.