What is a Stub? Stubbing in Programming and Testing
A stub is a controllable stand-in for a real dependency, returning predefined responses in tests. Learn stub vs mock and how to create API stubs with WireMock.
What is API Stubbing? How is it Different from Mocking?
Last updated: August 2026
In programming, a stub is a small piece of placeholder code that stands in for a real component — returning predefined responses so the rest of the system can be built and tested before, or without, the real thing. API stubbing applies the same idea to HTTP: a dummy implementation of API responses serves as a temporary substitute for the actual API.
Stubbed responses are one step above dummy data for testing or development, and one step below a mock API. Stubs should maintain the exact number of parameters and produce dummy results that are of the correct type to ensures that the testing process is both smooth and accurate, and provide developers with a reliable method for evaluating their code.
What’s a stub? More generally, stubs are controllable replacements for existing dependencies within a system, helping developers test their code without directly interacting with the actual dependency. As one commenter on Stack Overflow mentions, they act as a programmer’s “Lorem Ipsum” – allowing developers to write tests and run examples even before the function, service, or component is complete (and the same applies when a 3rd party service isn’t available).
Stubbing vs Mocking
Stubbing and mocking are used to isolate software components during testing. However, there are some differences between the two techniques: Primarily, stubs are focused on providing predefined responses to specific input parameters, while mocks are geared towards validating the interactions or behavior of components.
In other words, stubbing is mainly used to replicate the behavior of an API in a controlled manner with predefined responses, which allows the developer to focus on testing the integration and functionality of the code without worrying about external dependencies. On the other hand, mocking involves creating a mocked version of a dependency and asserting that the component being tested interacts with this mock as expected, often concentrating on the expected sequence of calls, data manipulation, or side effects. While both stubbing and mocking help streamline the testing process and ensure accurate results, the choice between them depends on the specific goals and requirements of the test scenario.
| Stub | Mock | |
|---|---|---|
| Purpose | Supplies predefined responses | Verifies interactions |
| Answers | ”What does the dependency return?" | "Was the dependency called correctly?” |
| Testing style | State-based | Behavior-based |
| Typical use | Isolate code from a dependency | Assert calls, arguments, side effects |
API Stubbing in WireMock
WireMock is an open source framework designed for creating HTTP-based API stubs and mocks. It provides a simple way to define stubbed responses with various matching criteria. Using WireMock, you can create stubs for your API, control their behavior, and use them for testing. You can also use WireMock Cloud to define your stubs in a GUI and benefit from other features available only in the SaaS version.
Here’s a basic stubbing example using WireMock in Java:
@Testpublic void exactUrlOnly() { stubFor(get(urlEqualTo("/some/thing")) .willReturn(aResponse() .withHeader("Content-Type", "text/plain") .withBody("Hello world!")));
assertThat(testClient.get("/some/thing").statusCode(), is(200)); assertThat(testClient.get("/some/thing/else").statusCode(), is(404));}In this example, we set up a stub for the relative URL “/some/thing” that, when matched, returns a response with a status of 200, “Hello world!” as the body, and a “Content-Type” header set to “text/plain”. You can also create and configure stubs using JSON:
{ "request": { "method": "GET", "url": "/some/thing" }, "response": { "status": 200, "body": "Hello world!", "headers": { "Content-Type": "text/plain" } }}API stubbing in WireMock supports various request matching options, response templating, fault simulation, stateful behaviors, and proxying. Stubbing can also be used with different HTTP methods, including GET, POST, PUT, DELETE, HEAD, TRACE, and OPTIONS. Moreover, you can set the priority for your stubs to control the order in which they are executed when multiple stubs match the same request.
Read the full documentation:
- Stubbing in open source WireMock
- Stubbing in WireMock Cloud
Frequently asked questions
A stub is placeholder code that mimics a real function, service, or API by returning fixed, predictable responses. Developers use stubs to test code in isolation when the real dependency is unavailable, incomplete, or costly to call.
Stubbing provides predefined responses to specific inputs, focused on replicating behavior. Mocking additionally validates the interactions themselves — asserting that a component called the dependency in the expected way, with the expected data.
A stub is a simpler, more limited concept — a controllable replacement for a dependency. A mock API is generally a step up in sophistication, simulating fuller API behavior. Stubbing sits between raw dummy data and a full mock.
WireMock stubs support all standard HTTP methods — GET, POST, PUT, DELETE, HEAD, TRACE, and OPTIONS — along with request matching, response templating, fault simulation, stateful behaviors, and proxying.
Yes — WireMock lets you set a priority on stubs to control which one is used when multiple stubs match the same incoming request.