Quick revision
The short answer
I would move ownership of stubs from the consumers to the providers. Platform teams publish stubs generated from their contracts or OpenAPI specs, for example Spring Cloud Contract stub jars or Prism mock servers, and consumers test against those published stubs while providers verify the same contracts in their own pipelines. For shared environments, a virtualisation service serves versioned stub sets. Drift is measured by running stub-based tests against real sandboxes on a schedule, and team-specific stubs are retired as the published ones cover each team's needs.
What to remember
In this answer, we fixed disagreeing stubs by changing who owns them. Providers publish stubs generated from their contracts or OpenAPI specs, such as Spring Cloud Contract stub jars or Prism mock servers, consumers test against them, and providers verify the same contracts in their pipelines. A shared virtualisation service serves versioned stub sets to environments, scheduled comparisons against real sandboxes measure drift, and team-specific stubs are retired as published ones cover each team's needs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would move ownership of stubs from the consumers to the providers. Platform teams publish stubs generated from their contracts or OpenAPI specs, for example Spring Cloud Contract stub jars or Prism mock servers, and consumers test against those published stubs while providers verify the same contracts in their own pipelines. For shared environments, a virtualisation service serves versioned stub sets. Drift is measured by running stub-based tests against real sandboxes on a schedule, and team-specific stubs are retired as the published ones cover each team's needs.
Why this question is asked
When twenty teams each write their own fake version of the same core APIs, the organisation ends up with twenty slightly different ideas of how those APIs behave, and none of them is checked against the real thing. Integration bugs then appear only in shared environments or production. Interviewers ask this to see whether we can fix a problem that is organisational as much as technical. In this answer, we will look at why consumer-owned stubs drift, the model of provider-published stubs, how contracts keep both sides honest, the shared virtualisation service, how to measure drift, and how to migrate twenty teams.
1Why Consumer-Owned Stubs Drift
A stub is a simulated response a test uses in place of the real API. When a consumer team writes its own stubs, it writes down what it believes the API does at that moment, often by copying a response from a log or from the documentation. From then on, nothing connects the stub to the real API. When the provider adds a required field, changes an error format or starts returning 409 instead of 400, the twenty sets of stubs stay as they were.
So each team's tests pass against its own fiction. The disagreement between stubs is a symptom: if twenty teams model the same endpoint twenty ways, at most one of them can be right, and probably none is fully right. The cost appears late, in shared environments or in production, where it is the most expensive to find.
2Make Providers Publish the Stubs
The structural fix is to have one source of truth per API, owned by the team that owns the API. That team already knows how its API behaves and is the only team that knows when it changes. There are two common ways to publish stubs from a single source.
With Spring Cloud Contract, the provider writes contracts, small definitions of a request and the expected response, in YAML or a Groovy or Java DSL. From those contracts the build generates tests that run against the provider's real implementation, and a stub jar containing WireMock mappings that is published to the artifact repository. Consumers add the stub jar as a test dependency and run it with the Stub Runner, so their tests talk to stubs that the provider has proven match its code. With an OpenAPI-first approach, the provider publishes its specification, and a tool such as Prism runs a mock server from it, returning responses that conform to the schema and the examples in the specification. This is less precise than contracts, because it checks shape rather than behaviour, but it is cheap for providers that already maintain good specs.
3Contracts Keep Both Sides Honest
Publishing stubs is not enough on its own; we need both sides to rely on the same definition. Consumers test against the published stubs. Providers verify the same contracts against their real code in every build. If a provider change breaks a contract, the provider's own pipeline fails, before any consumer is affected.
Consumer needs flow back the other way. When a consumer needs behaviour that no contract covers, such as a specific error case, it proposes a new contract to the provider, through a pull request to the provider's contract folder. With Pact, the consumer-driven variant, consumers generate contracts from their tests and a broker shares them with providers, who verify them; the broker can then tell whether a given consumer and provider version are compatible before deployment. Either model works. What matters is that one definition is checked on both sides.
6 messages in order. Play them, or step through them one at a time.
The stub a consumer uses has already passed against the provider's real implementation.
4A Shared Virtualisation Service for Environments
Tests in a build can load stub jars directly, but shared and ephemeral environments need running virtual services. A platform team can run a virtualisation service: WireMock or Prism instances that serve each API's published stub set, selected by version. An environment for a feature branch can say, for example, customer-api stubs 4.12.0 and billing-api stubs 7.3.1, and get exactly those.
Versioning matters. A consumer on billing-api 7.3 should not suddenly receive stubs for 8.0 because the provider published them. Stub sets are tagged with the API version, kept for as long as that version is supported, and the service records which environments use which version, which also tells providers who still depends on old versions.
5Measuring Drift and Retiring Old Stubs
Even with contracts, some drift remains: contracts cover only the interactions someone wrote down, and OpenAPI mocks check shape, not behaviour. So I would run a scheduled job, nightly or weekly, that replays a sample of stub-based scenarios against the providers' real sandboxes and compares the responses with the stubs. The drift rate, the share of scenarios where real and stubbed responses disagree in status or required fields, is published per API. A rising number tells a provider its contracts are missing cases.
Migration is gradual. We start with the two or three most used platform APIs, publish their stubs, and help a few consumer teams switch. Each team then deletes its own stubs for that API once the published ones cover its cases, and gaps become contract proposals. A simple inventory, counting hand-written stubs per API in each repository, shows progress. Over two or three quarters, the number of private stub sets for core APIs should approach zero, and with it the disagreements.
| Aspect | Twenty private stub sets | Provider-published stubs |
|---|---|---|
| Source of truth | Each consumer's belief | Provider's contracts or OpenAPI spec |
| Checked against real API | Never | In the provider's build, every change |
| When an API changes | Stubs silently out of date | Provider build fails or new version published |
| Error cases | Whatever each team guessed | Proposed as contracts, shared by all |
| Drift visibility | None | Scheduled sandbox comparison per API |
6Follow-up questions
What if a provider team refuses to own stubs?
Make it part of the platform's definition of a published API, alongside documentation and versioning, and give providers tooling that makes it cheap, such as a build template that generates and publishes stubs from contracts. In the meantime, a platform team can own contracts for that API and verify them against its sandbox, so consumers still share one set.
Spring Cloud Contract or Pact?
Spring Cloud Contract fits well when providers are Spring teams and are willing to own contracts, since it generates both tests and WireMock stubs. Pact fits consumer-driven workflows across languages, with a broker that tracks which versions are compatible. Large organisations sometimes use both; the important thing is one definition per interaction, verified by the provider.
Does this remove the need for integration environments?
It reduces how much depends on them. Teams can run most of their tests against published stubs, quickly and reliably. Shared environments remain useful for end-to-end checks of critical flows and for testing operational behaviour, but they stop being the only place where integration problems surface.
7Conclusion
In this answer, we fixed disagreeing stubs by changing who owns them. Providers publish stubs generated from their contracts or OpenAPI specs, such as Spring Cloud Contract stub jars or Prism mock servers, consumers test against them, and providers verify the same contracts in their pipelines. A shared virtualisation service serves versioned stub sets to environments, scheduled comparisons against real sandboxes measure drift, and team-specific stubs are retired as published ones cover each team's needs.
Open with
Twenty sets of stubs for the same APIs means twenty beliefs about how those APIs behave, and nothing checks any of them against the real code. So I would change ownership: the provider team, which knows when its API changes, publishes the stubs.
Then, if they want more
If providers use Spring Cloud Contract, their contracts generate both tests that run against the real implementation in their build and a stub jar with WireMock mappings. Consumers add that stub jar as a test dependency and run it with the Stub Runner, so the stubs they use have already been proven against the provider.
For teams that are OpenAPI-first, a Prism mock server from the spec is a cheaper option, though it checks shape more than behaviour. When a consumer needs a case the stubs do not cover, like a specific error, it proposes a contract to the provider, so everyone gets it.
For shared and ephemeral environments, a platform virtualisation service serves versioned stub sets, so an environment can pin the API versions it uses. Because contracts never cover everything, I would run a scheduled job comparing stub responses with the real sandboxes and publish a drift rate per API.
Then I would migrate gradually, starting with the most used APIs, and retire each team's private stubs as the published ones cover its needs.
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.
- Official documentation
- JUnit user guide, Mockito documentation
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 "Twenty teams each maintain their own stubs for the same core platform APIs, and stubs...": https://scrivio-interview-library.netlify.app/java/test-strategy/a51cd87b64/