Skip to content
Preptima
mediumConceptCodingDesignMidSenior

How do you test a service that calls a third-party API, without calling it?

4 min readUpdated 2026-08-15

Answer

Short answer

Substitute at the network layer or behind your own client interface, not at the HTTP library's function. Stubbing the library couples the test to how you make the call rather than to what you send, so the test keeps passing through refactors that change the request and breaks on refactors that change nothing.

Was this answer helpful?

Likely follow-ups

  • Your fake returns 200 and the real API started returning 202. What catches that?
  • How do you test the timeout path without waiting for the timeout?
  • When would you record real responses instead of hand-writing them?
  • Where do contract tests fit if the provider is a third party you cannot influence?

Related questions