Skip to main content

Stubbing

Stubbing provides a hard-coded, canned response in place of a real dependency's behavior — simpler than a full mock, since a stub doesn't verify how it was called, it just returns a fixed answer whenever it's invoked. It's used to isolate the code under test from dependencies that aren't relevant to what's actually being tested.

The distinction from mocking is subtle but real: a stub exists purely to make a test runnable by supplying a needed value ("this function returns 42 no matter what"), while a mock additionally lets the test assert on the interaction itself ("this function was called exactly once, with these arguments"). In casual conversation the two terms often get used interchangeably.

Stubbing is especially common in incremental integration testing — before a real module exists or is integrated yet, a stub stands in for it, letting testing of already-integrated modules proceed without waiting for every dependency to be ready.