Prompt
What should I use for mocking downstream APIs?
Latest observation
For mocking downstream APIs, the best choice depends on what level of testing you want:
1) Unit tests: mock at the client/interface boundary
Use a mocking library in your language/framework:
- Java: Mockito, WireMock (if you want HTTP-level mocks)
- Python:
unittest.mock,responses,pytest-httpx - JavaScript/TypeScript: Jest mocks, MSW, Nock
- .NET: Moq, WireMock.Net
Use this when you just want to verify your code’s behavior without real HTTP calls.
2) Integration tests: HTTP-level mocking
Use something that mocks the actual HTTP contract:
- WireMock / WireMock.Net
- MockServer
- MSW (great for browser/frontend and some Node use cases)
This is better when your code talks to downstream services over HTTP and you want realistic request/response matching.
3) Contract testing: verify expectations between services
Use:
- Pact
- Spring Cloud Contract
This is ideal when you want to ensure your service and the downstream API agree on the contract, without needing the real service running.
4) Full local simulation
Use a local fake service or containerized test environment:
- lightweight fake server you control
- Testcontainers + real dependent services in containers
This is useful when behavior is too complex to mock well.
Practical recommendation
If you’re mocking downstream HTTP APIs, a strong default is:
- Unit tests: mock the client object directly
- Service/integration tests: WireMock or MSW depending on stack
- Cross-team API confidence: Pact
Rule of thumb
- Need speed and simplicity? Library mocks
- Need realistic HTTP behavior? WireMock / MSW / MockServer
- Need API compatibility guarantees? Pact
If you tell me your language/runtime and whether this is for unit, integration, or contract tests, I can recommend the best specific tool.