Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Your service depends on six external APIs, and the shared test environment is down half the time. How would you use service virtualization in your test strategy, and what are the risks?

I would virtualise all six dependencies with a tool such as WireMock, Hoverfly or Mountebank, in integration tests and in local and ephemeral environments, so our tests no longer depend on the shared environment. Stubs are bootstrapped by recording real interactions, then curated and extended with error, timeout and slow responses. The main risk is that stubs drift from reality, so I would pair them with contract tests (Pact or Spring Cloud Contract) or periodic runs against real sandboxes, keep stubs versioned and owned, ideally published by the provider teams, and remember that latency in performance tests with virtualised dependencies is synthetic.

Why this question is asked

A shared test environment that is down half the time is a common reason why delivery slows: pipelines wait, tests are rerun, and nobody trusts a red build. Service virtualisation, replacing a dependency with a simulated one, is the standard answer, but it brings its own risk of testing against a fiction. Interviewers ask this design question to see whether we can use virtualisation and keep it honest. In this answer, we will recap service virtualisation, design where it is used, see how stubs are created and curated, deal with drift through contracts and sandbox checks, decide on ownership, and look at performance testing.

1A Quick Recap of Service Virtualisation

Service virtualisation means running a simulated version of a dependency that answers like the real one. Our service calls it over HTTP exactly as it would call the real API, so our code, configuration and network handling are exercised, but the answers are under our control. WireMock is the most common choice in Java; Hoverfly is good at recording and replaying traffic as a proxy; Mountebank supports several protocols beyond HTTP.

With six external APIs and a shared environment that is unavailable 50 percent of the time, the chance that all six dependencies and the environment are healthy at the moment a pipeline runs is low. Virtualisation removes that dependency for most of our tests, which turns a build failure back into a signal about our code.

2Where Virtualisation Is Used

I would use it at three levels. In integration tests, each test starts an in-process WireMock server and stubs exactly the responses that test needs, including failures. In local development, a docker-compose or Testcontainers setup starts virtual versions of all six APIs from stub files, so an engineer can run the whole service on a laptop. In ephemeral environments, created per pull request, the same virtual services stand in for the dependencies, so end-to-end tests of our service run in isolation.

The shared environment does not disappear. It is still useful for occasional checks with real dependencies, but it stops being on the critical path of every pull request.

Step throughTest levels and what each one talks to
  1. Unit testsno network; fakes for clients
  2. Integration testsin-process WireMock per test, including errors and delays
  3. Local and ephemeral environmentsvirtual services from shared stub files
  4. Contract verificationproviders check the same contracts in their pipelines
  5. Scheduled sandbox checksa small suite against real provider sandboxes

5 layers, from the top down. Step through them one at a time.

Virtualisation covers the fast levels; contracts and sandbox checks keep the stubs honest.

3Creating and Curating Stubs

Writing stubs for six APIs by hand is slow, so we bootstrap them by recording. WireMock can run as a proxy in front of a real sandbox and record each request and response as a mapping file; Hoverfly does the same in capture mode. A day of running our existing test flows through the recorder gives us a first set of realistic responses.

Recordings are a starting point, not the finished product. They contain noise, such as timestamps, generated ids and duplicate calls, and they only include the happy paths that happened during recording. So we curate them: replace volatile values with templates or loose matchers, remove duplicates, and give each mapping a clear name. Then we add what recording never captures: 4xx validation errors, 500 and 503 responses, malformed bodies, slow responses with a fixed delay, and connection resets. Those are the cases where our retry, timeout and fallback logic actually runs, and the shared environment could never produce them on demand.

JSON
{
"name": "credit-check: bureau unavailable",
"request": { "method": "POST", "urlPath": "/v2/checks",
"bodyPatterns": [{ "matchesJsonPath": "$[?(@.customerId == 'slow-001')]" }] },
"response": { "status": 503, "fixedDelayMilliseconds": 3000 }
}

Stub files like this one live in version control, are reviewed like code, and are loaded the same way by integration tests and by the local environment.

4The Main Risk: Stubs That Drift From Reality

The danger of virtualisation is that the stubs describe how the API behaved when we recorded it, or how we believe it behaves. Providers change: a field becomes required, an enum gets a new value, an error format changes. Our tests stay green against the stubs while production breaks. This is called drift, and it is the reason some teams distrust virtualisation.

Two techniques control it. Contract tests make the expectations explicit and check them on both sides. With Pact, our tests record what we expect from the provider as a contract file, and the provider's pipeline verifies that its real implementation satisfies it. With Spring Cloud Contract, the provider writes contracts from which both provider tests and stub jars are generated. Either way, a provider change that would break us fails in the provider's build. For external APIs whose owners will not run our contracts, the second technique is a scheduled suite, for example nightly, that runs a small number of key scenarios against the provider's real sandbox and compares the responses with our stubs. A mismatch is a ticket to update the stubs, before it is an incident.

Step throughKeeping stubs honest
  1. Record real interactionsproxy mode against a sandbox
  2. Curate and add failurestemplates, 4xx, 503, delays
  3. Use stubs in tests and environments
  4. Does the provider run our contracts?
    YesContract verification in their pipelineNoNightly checks against their sandbox
  5. Mismatch foundupdate stubs, review, release a new version

5 steps in order. Play them, or step through them one at a time.

Recording gives realism, curation gives coverage, contracts and checks give trust.

5Ownership, Versions and Performance Tests

Stubs need owners, or they rot. For internal APIs, the best owner is the provider team, which publishes versioned stubs alongside the API, for example Spring Cloud Contract stub jars or a mock server generated from its OpenAPI specification. For external vendors, our team owns the stubs and the drift checks, with one named person per dependency. Either way, stubs are versioned, so a consumer can pin the version matching the API version it uses.

Virtualisation is also useful in performance tests, because it isolates our service: we measure our own throughput and latency without the noise of six dependencies. But we must remember that the dependency latency is synthetic. If the stubs answer in 5 milliseconds and the real credit bureau takes 400 milliseconds at the 99th percentile, our connection pools and thread counts will look far healthier than they are. So we configure realistic delays, ideally distributions taken from production metrics, and still run occasional performance tests against real dependencies before major releases.

6Follow-up questions

Which tool would you pick among WireMock, Hoverfly and Mountebank?

For a Java team testing HTTP APIs I would start with WireMock, because it integrates with JUnit and Spring, supports recording, templating and fault injection, and has a Testcontainers module. Hoverfly is attractive when recording through a proxy is the main need. Mountebank fits when non-HTTP protocols such as raw TCP need simulating.

Do contract tests replace virtualisation?

No, they complement it. Contracts verify that the provider still behaves as the stubs claim, while virtualisation lets us run fast, deterministic tests of our own service, including failure cases the provider would never produce on demand. Spring Cloud Contract even generates the stubs from the contracts, which joins the two.

How do you test failure modes like connection resets?

WireMock supports faults such as CONNECTION_RESET_BY_PEER and EMPTY_RESPONSE in the response definition, alongside fixed and random delays. Using them in integration tests is the reliable way to check timeouts, retries and circuit breakers.

7Conclusion

In this answer, we designed a test strategy around service virtualisation for six unreliable dependencies. We used WireMock-style virtual services in integration tests and in local and ephemeral environments, bootstrapped stubs by recording and curated them with error, timeout and slow responses, controlled drift with contract tests or scheduled sandbox checks, kept stubs versioned and owned, ideally by providers, and treated latency in virtualised performance tests as synthetic.

What to say in the interviewAbout 90 seconds aloud

Open with

With six dependencies and a shared environment that is down half the time, our pipeline mostly reports on the environment, not our code. So I would virtualise the dependencies.

Then, if they want more

For Java I would use WireMock: in integration tests, each test stubs exactly what it needs, and in local and per-pull-request environments, virtual services start from the same stub files.

To create the stubs quickly, I would record real interactions through WireMock in proxy mode against the sandboxes, then curate them, replacing timestamps and ids with matchers and templates, and add what recording never captures: validation errors, 500s and 503s, slow responses and connection resets, because that is where our retry and timeout code runs.

The main risk is drift: stubs describe what the API did when we recorded it, and the provider changes. I would handle that with contract tests, Pact or Spring Cloud Contract, where the provider team verifies our expectations in its pipeline, and for vendors who won't, a nightly suite against their real sandbox that flags differences.

Stubs live in version control, are versioned and have owners, ideally the provider team publishing them. Virtualisation also helps performance tests by isolating our service, but the dependency latency is synthetic, so I would configure realistic delays from production metrics and still test against real systems before big releases.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your service depends on six external APIs, and the shared test environment is down half...": https://scrivio-interview-library.netlify.app/java/test-strategy/b9f90feb0f/