# Stub (Testing)

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

**Field:** Software Development  
**Category:** Testing & Quality  
**Also known as:** Stub, Test Stub, Stubbing

## Think of it like

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

## 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.

## 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.

## Common mistake

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

## Three principles

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

## Related terms

- [Test Double](https://learn.benibix.com/term/development/test-double)
- [Mock (Testing)](https://learn.benibix.com/term/development/mock)
- [Fake (Testing)](https://learn.benibix.com/term/development/fake)
- [Unit Test](https://learn.benibix.com/term/development/unit-test)

## Told apart from

- [Mock vs Stub](https://learn.benibix.com/compare/mock-vs-stub): A mock records how your code called it, and the test checks those calls afterwards. A stub only hands back an answer you chose ahead of time and checks nothing. Use a stub to set up a case, and a mock to prove a call happened.
- [Fake vs Stub](https://learn.benibix.com/compare/fake-vs-stub): People think a fake is the same as a stub, and it is not. A stub hands back a fixed answer, whatever you ask it. A fake really works, just in a small way, such as a pretend bank that keeps balances in memory. Put money in a fake and you can take it out again. A stub cannot do that.

---

Source: https://learn.benibix.com/term/development/stub
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
