API tests

Mock, sandbox or real service: how to test external integrations

A payment gateway, carrier or partner API may be slow, unavailable, or capable of creating real side effects. A mock can speed up feedback but cannot show that the real systems understand each other. Use each layer to answer a different question.

Why one testing layer is not enough

A mock is a controlled substitute for the external service. It can return a predefined response or error, or introduce a controlled delay, making timeouts, invalid payloads, and outages repeatable.

Mocks are fast and deterministic: the same input can produce the same condition. They prove only how the application behaves against the team’s programmed interpretation of the interface. A test can pass when the mock uses an outdated field or incorrect status code while the real connection remains broken. Our API testing checklist describes useful error conditions to cover at this layer.

A sandbox is a test environment operated by the provider. You interact with the provider’s software and authentication mechanisms using test accounts. Check permitted operations and side effects against the provider’s rules and your own application. It is suitable for checking request formats, credentials, callbacks, and the states that the provider makes available.

A sandbox may not reproduce production data, capacity, timing, fraud controls, or every feature. Some outcomes may themselves be simulations. Payment integrations need an explicit understanding of what a gateway’s test mode cannot prove rather than treating one sandbox transaction as evidence for the live process.

The real service gives stronger evidence that the current configuration and connection work for a controlled scenario. A check may modify data, send messages, consume a quota, or fail outside your product. Respect provider rules, run it narrowly and safely, agree it with operations, and limit its frequency according to its side effects.

Choose according to the question, not apparent realism

State what the result needs to demonstrate before choosing a layer:

Also consider test-data ownership, permitted side effects, call limits, environment availability, and required feedback speed. The most realistic test is not automatically right for every change.

Combine layers to reduce blind spots

Begin with broad mock coverage: success, known errors, a timeout, invalid data, retries, and partial completion. These scenarios can run without the partner.

Anchor the mock in an interface agreement. An OpenAPI schema can check message shape, while a consumer-driven contract records the client’s specific expectations. Tools such as Pact can verify recorded interactions against the provider implementation. A contract knows only about covered interactions, however, and does not replace the wider integration strategy.

Keep a representative successful journey and the important failure states supported by the sandbox. Add only essential checks against the real service. Label results so the team can distinguish a product defect from an unavailable dependency, as it must when stabilising intermittent tests.

Important limitations

Do not build a mock by copying one successful response without its rules. Preserve required fields, types, state transitions, and errors on which the application relies. When the contract changes, update the agreement first and then adjust both the mock and its tests.

A production check must not send uncontrolled customer emails, charge customers, or alter stock. Use dedicated accounts, recognisable test data, idempotent operations where the interface supports them, and an agreed clean-up process. Even a safe successful run covers only its selected state, not every production condition.

An example of dividing order checks between layers

An illustrative shop sends a payment request and waits for confirmation. A successful mock response, sandbox acceptance and a correctly saved order state are three different pieces of evidence. State which parts of the system each test replaces.

Illustrative question Layer Evidence and boundary
What does the user see after a declined payment? Controlled mock The app’s correct response, without evidence of gateway connectivity
Does the gateway accept our data and deliver a supported confirmation? Sandbox Format, credentials and confirmation handling in test mode
Does a repeated confirmation create a second order? Testing our processing with prepared events Final state and duplicate protection, rather than every provider condition
Does a permitted check work with current configuration? Real service, within permitted scope only The selected connection and state, rather than proof of the whole integration

Record unverified questions too, such as production processing time or a failure condition the sandbox cannot reproduce. Accepted gaps need an owner and next step. A more realistic layer does not fill every missing case on its own.

Establish where a request is replaced

In a browser test, a mock may replace the page’s connection to your backend. This also bypasses your server logic, rather than only the external provider. To test server-side payment processing, substitute at the server-to-partner boundary or in a separate server-logic test.

Playwright describes mocking page requests. Do not treat page.route as interception of every application call. Its documentation also notes requests handled by a Service Worker, which this method does not intercept. If you block the worker for a test, record that the run does not verify its behaviour.

Check that the report shows the expected substitute and correct data were used. A request bypassing the mock must not silently reach an unauthorised environment. Conversely, an overly broad replacement can conceal the defect you intended to test.

The contract must exercise real code too

Pact recommends using the actual application client in consumer tests. A test that sends a manually assembled message outside that client may miss its data-mapping defect. Then verify the contract against the provider implementation. Generating a contract file alone does not perform that second step.

For a service you do not control, establish whether that verification is available. Documented schemas and response samples can help check your own code, but without verifying the other side, do not describe the result as complete contract evidence. Record the interface version and test-data origin. When things change, decide which expectation changed rather than automatically replacing every sample with the latest response.

Sandbox and production have their own rules

Check the particular provider’s documentation. For example, Stripe requires test data and keys for testing and states that testing in live mode with real payment details is prohibited. A small amount or your own team’s agreement does not replace that rule. This is a Stripe example, rather than a rule shared by every gateway.

Stripe recommends controlled responses for automated checks of your application’s behaviour. Its payment-entry interfaces have protections against automated testing, and its APIs have rate limits. Checking provider requests in a test environment therefore has a separate scope. Do not use a testing sandbox as an unlimited load-test target.

Check your own downstream systems even with test accounts. An app can send an email or change stock after success regardless of where payment was simulated. Isolate those effects and agree on cleanup. For a failure, distinguish an app defect, invalid test data, unavailable partner and unclear state. A later successful attempt must not make an unclear outcome count as a pass.

What you gain

Next step

List your external dependencies and ask three questions about each one: which failure must the application handle, which agreement must remain compatible, and what can only be demonstrated through a real connection? Assign each question to the lowest layer that can answer it credibly, then document the limitations that remain.

Related topics

You might also be interested in

You can detect errors in the business logic more quickly directly through the API

Functional, integration and contract testing of REST, SOAP or GraphQL interfaces with the possibility of connection to the CI/CD pipeline.