Chapter 26Design-for-Testability Patterns

Patterns in This Chapter

Dependency Injection 678

Dependency Lookup 686

Humble Object 695

Test Hook 709

Dependency Injection

How do we design the SUT so that we can replace its dependencies at runtime?

The client provides the depended-on object to the SUT.

image

Almost every piece of code depends on some other classes, objects, modules, or procedures. To unit-test a piece of code properly, we would like to isolate the code from its dependencies. This isolation is difficult to achieve if those dependencies are hard-coded in the form of literal classnames.

Dependency Injection is a way to allow the normal coupling ...

Get xUnit Test Patterns: Refactoring Test Code now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.