liveBook · Manning

Seam

As explained in The Art of Unit Testing 3e

A seam is a location in code where behavior or dependencies can be replaced for testing.

A seam is a place where pieces of code meet and a test can replace a dependency or behavior. It gives the test control over indirect inputs such as time, files, networks, random values, or logging. section 3.3.2 section 1.8

A dependency is the thing that makes behavior hard to control; a seam is the access point used to control it. Code with accessible seams is usually easier to test, read, and maintain as production behavior changes. section 3.3.2

Create seams by abstracting a dependency in forms that fit the code:

Making a logger testable

A password verifier that imports a complicated logger directly is difficult to test with ordinary injection. Let the verifier receive a logger instead, then pass a fake logger whose info method records whether PASSED or FAIL was reported. The seam is the logger boundary, not the logging call itself. section 4.2

Seams improve test isolation: tests can receive consistent fake time, databases, networks, or machine settings rather than depend on their environment. This supports readable, maintainable tests whose results can be trusted. Missing seams are a common reason existing code is hard to test or safely refactor. section 12.0 section 1.8

Table 4.1 Techniques for injecting fakes (view table figure)

Style

Technique

Standard

Introduce parameter

Functional

Use curryingConvert to higher-order functions

Modular

Abstract module dependency

Object oriented

Inject untyped objectInject interface

Compare parameter, functional, modular, and object-oriented techniques for creating a boundary where tests can inject fakes. section 4.2

How The Art of Unit Testing 3e covers it

How deep it goes
Defines seams, contrasts them with dependencies, and catalogs four injection techniques without detailing their implementations.
Examples
Uses time, files, networks, random values, databases, and a password verifier’s logger as dependency examples.
What you can do after
Apply it via parameters, higher-order functions, module abstraction, or object injection to supply test fakes.
How it connects
Connects seams to dependencies, dependency injection, stubs, inversion of control, unit tests, integration tests, and legacy code.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Unit Testing 3e. The section links point to the parts of the book it draws on.