A typical digital lending app in India might depend on a credit bureau for scores, a KYC provider for document checks, a bank’s account aggregator interface, an e-sign service, an SMS gateway and a payment gateway. In a test environment, few of those behave well. Sandboxes go down at weekends, some charge per call, some return the same canned response no matter what you send, and almost none will let you simulate the failures that cause real production incidents. If every test needs all of them working, testing stops whenever any of them stops. Test doubles and service virtualisation are the answer to that dependency problem.

A vocabulary that helps

“Mock” is used loosely to mean any fake dependency. Gerard Meszaros, in xUnit Test Patterns, gave a more precise vocabulary under the umbrella term test double:

  • Dummy: an object passed only to fill a parameter, never actually used.
  • Stub: returns pre-programmed answers to calls. “When asked for the credit score of this PAN, return 742.”
  • Spy: a stub that also records how it was called, so the test can check afterwards.
  • Mock: set up with expectations about how it should be called, and fails the test if those expectations are not met. “The SMS service must be called exactly once with this mobile number.”
  • Fake: a working but simplified implementation, such as an in-memory database instead of a real one.

The distinction between stubs and mocks matters in practice. A stub lets you control what your code receives. A mock lets you verify what your code sends. Testing that a loan is rejected when the bureau score is low needs a stub. Testing that the rejection SMS was sent needs a mock or spy.

From in-code mocks to virtual services

Libraries such as Mockito in Java or the standard mocking module in Python create test doubles inside the code under test. They are perfect for unit tests: fast, precise and isolated. But they replace a dependency at the object level, so the real HTTP client, serialisation, timeouts and network code are never exercised.

Service virtualisation moves the fake out of the code and onto the network. A virtual service listens on a real host and port, accepts real HTTP (or messaging) requests, and returns configured responses. The application under test is pointed at it through configuration, with no code change, and behaves exactly as it would with the real provider. This makes it suitable for integration, system and performance testing, not only unit tests.

WireMock is a widely used open-source example for HTTP APIs. You define stub mappings that match incoming requests by method, URL, headers or body content, and specify the response to return. It can run inside a Java test, as a standalone process, or in a container. Mountebank and Hoverfly are other open-source tools in the same space, and large enterprises sometimes use commercial products that also handle protocols such as message queues or mainframe interfaces.

What virtualisation makes possible

The obvious benefit is availability: testing continues when the real sandbox is down. The deeper benefit is control, which lets you test behaviour that real sandboxes cannot reproduce on demand.

Failure modes. How does the lending app behave if the bureau takes 30 seconds to respond? If it returns an HTTP 500? If the connection is reset mid-response? If the response body is malformed? WireMock and similar tools can inject fixed or random delays and simulate connection faults. These are the scenarios behind real outages, and they are nearly impossible to trigger against a third-party sandbox.

Edge-case data. A bureau sandbox might only return a handful of test profiles. A virtual service can return a thin-file applicant with no history, a score exactly at the cut-off, or a response with an unexpected optional field.

Stateful flows. Payment flows often involve a pending status followed later by success or failure. Virtual services can be configured to return “pending” on the first status check and “success” on the third, letting you test polling and reconciliation logic.

Cost and rate limits. Running a 10,000-request performance test against a paid KYC API is expensive and may breach the provider’s terms. A virtual service costs nothing per call.

Where the responses come from

Virtual service responses can be hand-written from the provider’s API documentation, or recorded. In record mode, the virtualisation tool sits as a proxy between your application and the real sandbox, captures real request and response pairs, and saves them as stubs for later playback. Recording is quick and realistic, but recordings must be scrubbed of any real personal data and refreshed when the provider changes its API. Hand-written stubs are more deliberate and better for edge cases.

The main risk: a fake that drifts from reality

A virtual service is a model of someone else’s system, and models go stale. If the payment gateway renames a field or adds a mandatory header, your virtual service keeps returning the old format and every test stays green until production breaks. This is the central weakness, and teams handle it in a few ways:

  • Keep a small set of tests that run against the real sandbox regularly, perhaps nightly, to catch drift.
  • Where the provider is internal, use contract testing so both sides verify the same agreed interface.
  • Version stubs alongside the code and review them when provider release notes arrive.
  • Re-record periodically and compare against existing stubs.

Choosing the right level

A reasonable layering looks like this. Unit tests use in-code mocks and stubs for speed. Component and integration tests use virtual services so real network code is exercised and failures can be simulated. A thinner layer of end-to-end tests runs against real sandboxes to confirm that the real integration works. Before go-live, a few carefully controlled checks run against production endpoints, often with small real transactions that are then reversed, depending on what the provider allows.

The guiding question for any dependency is simple: what do I need to learn from this test, and does the real system help or hinder me in learning it? If you need to know how your code handles a gateway timeout, a virtual service is better than the real thing. If you need to know the real gateway accepts your request signature, nothing but the real thing will do.

Written by the RP Strativa Team. The lending app setup and score values are illustrative.

Leave a Reply

Trending