Understanding stubs: the building blocks of API simulation
The fundamental building block of a simulation is a stub. A stub represents a rule that tells WireMock how to respond when it receives certain kinds of requests.A stub consists of two main parts:
Request matching criteria - The conditions that an incoming request must satisfy to trigger this stub
Response definition - A recipe for generating and returning a response when the criteria match
When WireMock receives an HTTP request, it evaluates that request against each stub in order, from top to bottom. The first stub whose request criteria fully match the incoming request is selected, and its response definition is used to generate the response.This top-to-bottom matching order is important: more specific stubs should typically be placed before more general ones to ensure they are evaluated first.
Request matching can examine any part of an incoming HTTP request:
HTTP method (GET, POST, PUT, DELETE, etc.)
URL path and path patterns
Query parameters
HTTP headers
Request body content
Cookies
Matching can use simple equality checks or more sophisticated criteria:
Exact matches for precise control
Pattern matching with wildcards or regular expressions
Content-type specific matching (e.g., JSON path queries, XML XPath expressions)
Absence checks (matching when something is NOT present)
Multiple criteria combined with logical AND/OR operations
The flexibility of request matching allows you to create both broad catch-all stubs and highly specific stubs that only match very particular requests.