Skip to content
BENIBIX Learn

Stub (Testing)

Development🧪 Testing & QualityTest Doubles

Also known as Stub, Test Stub, Stubbing

A fake part that always gives the answer you chose, so tests stay the same.

Think of it like

Think of it like a parrot. It gives the same answer every time you ask.

Example

Lucy's app shows an umbrella when it rains. Her test uses a stub weather service that always returns rain, so the umbrella must show.

Why it matters

Real services change their answers. A stub lets you test each case on purpose, including the rare ones.

Definition

A stub is a test double that returns a fixed answer you chose ahead of time. It does not check how it was called; it just answers. You use it to put your code in a known state, like a weather service that always says rain. That makes a test give the same result every time. Where a mock checks the call, a stub only feeds the answer.

Common mistake

Using a stub and then checking how it was called. That is a mock's job; a stub only answers.

Key takeaways

  1. 1
    A fixed answer. Chosen ahead of time.
  2. 2
    Does not check calls. It only answers.
  3. 3
    Sets up a case. Rain, error, empty list.

Compare