Test stub
A test stub is a stand-in for a component that the code under test calls. It returns canned answers, fixed in advance by the test, so that the code can run without the real component, which may be slow, unfinished, costly to call or impossible to steer into the condition the test needs.
The longer version
Code rarely works alone. A function that converts a price into another currency asks a rate service for today's rate, and what the function returns depends on the answer. A test of that function cannot wait for the market to produce the rate it needs, and it cannot ask the live service to fail on request. A stub solves both problems. It takes the place of the rate service for the length of the test and answers every request with the value the test supplied: 1.25, or zero, or an error. Gerard Meszaros, whose book xUnit Test Patterns supplied the vocabulary of test doubles, describes the pattern as replacing a real object with a test-specific object that feeds the desired indirect inputs into the system under test. Indirect inputs are the values code receives from the things it calls, as distinct from the arguments it is handed. Martin Fowler, summarising that vocabulary in 2006, wrote that stubs provide canned answers to calls made during the test.
Meszaros sorts stubs by what they are for. A responder injects valid values so that the code can go about its business, and it serves the ordinary success tests. A saboteur injects invalid ones, an unexpected value or a raised exception, to show how the code copes when a dependency misbehaves. That is often the only practical way to run an error-handling branch, since a real payment provider or mail server cannot be told to time out on the third call. A temporary stub stands in for a component that has not been written yet: a shell with hard-coded return values, replaced by the real component once it exists. In each case the verdict of the test comes from the code under test, from the converted price, the message shown or the exception raised. The stub is never asked what happened to it.
The counterpart of a stub is a driver. A stub sits below the code under test and is called by it. A driver sits above and does the calling, in place of a caller that is missing or awkward to use. The ISTQB glossary defines a driver as a component or tool that temporarily replaces another component and controls or calls a test item in isolation, and a stub as a type of test double providing predefined responses. The pair marks the two directions in which a system can be assembled for testing: working from the top down needs stubs for the lower layers not yet built, and working from the bottom up needs drivers for the upper ones. A unit testing framework that calls a function and collects its result is acting as a driver. A stub stops where the test starts asking questions of the stand-in. Once the test checks which calls were made, how often or with what arguments, the object has taken on the job of a mock or a spy, and the mock vs stub entry sets out that contrast. A stub has one standing risk: it answers as its author believed the real component answers, and it goes on doing so after the real component has changed.
Putting a stub in place takes some design. The code under test has to receive its dependency from outside instead of creating it, so that the test can hand it the stand-in. Microsoft's guide to stubs in its Fakes framework recommends making a component depend on interfaces and not on concrete classes, and passing the dependency in, the practice called dependency injection. Its example gives a StockAnalyzer class a stub of its stock feed that always returns 345, because the real feed returns a different price on every call. The same test also stores the company code the stub was asked for and asserts on it, which in Meszaros's terms makes that stand-in a spy as well. The guide adds that sealed classes and static methods cannot be stubbed this way and need its shims instead. JavaScript test runners make the swap at run time: Steve Kinney's testing course replaces the global fetch function with a Vitest vi.fn() that resolves to fixed concert details. The stand-in can also be a whole service. Wikipedia notes that stubs can be built as a stub server or service, a separate process that answers requests with canned responses, which lets a web front end be tested before the API behind it is finished.
Common questions
What is a test stub?
A test stub is a replacement for something the code under test depends on, set up to give fixed, predetermined answers. The test installs the stub in place of the real component, runs the code and checks the code's result. The ISTQB glossary defines a stub as a type of test double providing predefined responses.
What is the difference between a stub and a driver?
A stub replaces a component that the code under test calls, and a driver replaces the component that calls the code under test. The stub supplies answers on the way down, and the driver supplies the calls and collects the results on the way up. A test of one module in the middle of a system may need both: a driver to invoke it and stubs for what it invokes.
When should a test use a stub?
A stub fits when the real dependency is slow, unavailable, not yet built, charged for by the call, or unable to produce the condition under test on demand, such as a timeout or a malformed reply. Gerard Meszaros names a stub used for that last purpose a saboteur. Where the real component is fast and dependable, a test that uses it proves more than a test that replaces it.
Where does a stub end and a mock begin?
The line is drawn by what the test verifies. With a stub the test checks only what the code under test produced, and the stand-in is never inspected. When the test goes on to check the calls the stand-in received, the object is doing the work of a mock or a spy. Mocking libraries can create both kinds, which is one reason the names get exchanged.
What are the risks of using stubs?
A stub encodes its author's belief about how the real component behaves. If the belief was wrong, or the component later changes its format or its errors, tests go on passing against behaviour that no longer exists. Stubs that only ever return success also leave failure paths unrun. Contract tests against the real interface, and a few tests that use the real component, limit both risks.
What does stub mean in programming?
In programming a stub is a short placeholder that stands where fuller code will be: a function with the right name and signature whose body does almost nothing, often returning a fixed value. Developers write stubs to let the rest of a program compile and run before a part is built. A test stub is the same idea used deliberately in a test, to give the code under test predictable answers.
How do you create a test stub?
Write or generate an object that has the same interface as the real dependency and returns the values the test needs, then give it to the code under test in place of the real one, usually through a constructor or function parameter. Frameworks automate the first part: Microsoft Fakes generates stub classes from interfaces, and in Vitest vi.fn() creates a function whose return value the test sets.
Bug reports that arrive ready to triage.
Collect replay, console, network and browser context with the Free plan.