Skip to content

Stub

A stub is a test double that returns canned answers to calls made during a test, standing in for a dependency the test does not want to exercise; outside testing, a stub is a short placeholder awaiting expansion.

Definition

What it is

A stub is the simplest useful test double. You give it a fixed response; it gives that response back whenever the code under test calls it. It does not verify that it was called, it does not record interactions, and it does not fail the test on its own. Its only job is to keep the test running.

Outside testing, "stub" means a placeholder. A Wikipedia stub is an article too short to be complete. A ticket stub is the half you keep after tearing. An API stub is an endpoint that returns a fixed shape before the real implementation exists.

Why it matters

When a test depends on a payment gateway, a clock or a remote API, the test becomes slow and fragile. A stub replaces that dependency with a deterministic answer, so the test measures the unit under test, not the network.

Stub vs mock vs fixture

Aspect

Stub

Mock

Fixture

Purpose

Provide canned input

Verify interactions

Prepare known state

Fails test on its own?

No

Yes, if expectation unmet

No

Records calls?

No

Yes

No

Typical use

Replace a dependency's return value

Assert a method was called

Seed a database, create files

Origin of the term

Gerard Meszaros (test double)

Meszaros, popularized by Fowler

xUnit tradition

Where stubs live

graph TD
  TD[Test Double] --> D[Dummy: never used]
  TD --> S[Stub: canned answers]
  TD --> SP[Spy: records calls]
  TD --> M[Mock: asserts expectations]
  TD --> F[Fake: working shortcut]

How to write one

Writing a stub that ages well

  1. Fix the contract

    Copy the real signature. If the real method returns a Promise, the stub returns a Promise.

  2. Hardcode the answer

    No randomness, no time, no network. Same input, same output, always.

  3. Do not assert on the stub

    A stub is not a spy. If you need to check a call, you want a mock.

  4. Keep it close to the test

    A stub that lives in a shared folder drifts. Keep it next to the test that uses it.

Origin

The word entered English around the 12th century as stubbe, a stump or the trunk of a felled tree. By the 16th century it meant any short remaining piece. That physical sense survives in "pencil stub", "ticket stub" and "cigarette stub". In the 20th century it became a metaphor for anything incomplete: a Wikipedia stub, a database stub, a test stub. The common thread is: something shorter than the whole, kept so the whole can be recognised.

Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.

— Martin Fowler, *Mocks Aren't Stubs*, 2007

Evidence

FAQ

Is a stub the same as a mock?

No. A stub returns a fixed answer and does not check whether it was called. A mock does check, and fails the test if the expectation is not met.

Is a fake a stub?

A fake has working logic, usually a simpler implementation (an in-memory database). A stub has no logic, only canned answers.

Can a stub fail a test?

Only if the stub itself throws. It does not fail on unmet expectations, because it has none.

When is a stub enough?

When the test only needs a dependency to return something plausible so the unit can continue.

Connections

Etymology

Origin: inherited

  • stub (noun): from Middle English stubbe, a tree stump; a short remaining part

Semantic network

StubTest doublsoftware-tMockFixtureSpyFake

● broader ● narrower ● related

Broader

Related

Used in the garden

Sources