Service virtualization means running a stand-in for a service your code depends on. Your code keeps its real HTTP client and sends its real requests. Only the base URL changes, and the stand-in answers. Its answers are recorded from the real service or written by you. WireMock, Mockoon, Prism, MockServer and Hoverfly all work this way. They make tests fast and repeatable, and they free a test from accounts someone else controls.
We ran into the edge of the idea while fixing a refund agent. It had refunded a customer twice. The fix was one line, and that line changed the question the agent asked Stripe. Before, it read only the latest refund in the account. After, it asked for the refunds on one payment. A recording of the broken run has no answer for the new request, because the broken agent never sent it. So the test you want most after a fix, the one that says the fix works, is the one a recording can’t run.
None of these tools is doing anything wrong here. The cause is what an agent is. An agent picks its calls while it runs, and the agents worth testing write: they refund payments, file tickets and merge pull requests. Ancha, which we make, is one of the stateful options, and the refund test below runs on it.
What is service virtualization?
Wikipedia describes a virtual service as an asset that listens for requests and returns an appropriate response. There are two ways to build one. You can record: put a proxy between your code and the real service, let them talk once, and save the traffic as stubs. Or you can write: describe each response by hand, or generate the responses from an API description such as an OpenAPI document.
The difference from a mock is where the stand-in sits. A mock replaces an object inside your process, so your HTTP client never runs. A virtual service runs outside the process. Your client, its serializer, its headers and its retry logic all run as they do in production. For an agent, that means its tool code is exercised for real, which a mocked client can’t give you.
Five open-source tools cover most of the HTTP work:
| Tool | Where the responses come from | State between requests |
|---|---|---|
| WireMock | Stubs written in code, through its REST API or as JSON files, or recorded through its proxy | Scenarios: states you name, and stubs that move a scenario from one state to the next |
| Mockoon | Routes you define in its desktop app or CLI, or routes generated from logged traffic | CRUD routes over a data bucket, which keep the records a client creates |
| Prism | An OpenAPI document: its examples, or values generated from its schemas | None: each response comes from the document |
| MockServer | Expectations you create, or a proxy that records requests and responses | Scenarios, each a named state machine |
| Hoverfly | Simulations captured in its Capture mode, or written by hand | A map of keys and values that request matchers read and change |
We took each row from the tool’s own docs on November 10, 2026.
Four of the five keep state. In WireMock, MockServer and Hoverfly the state is something you design. You name the states, decide which request moves between them, and write what each request returns in each state. Mockoon’s CRUD routes go further: a record a client creates shows up in the next list. WireMock and MockServer also keep a journal of every request they receive. A test can then assert that a request was made, made an exact number of times, or never made at all.
Where is service virtualization the right tool for an agent?
Anywhere the test decides what gets sent. In agent work that happens in four places, and in all four we reach for one of these tools first.
The first is the agent’s tool functions. A refund_order tool builds a request, sends it and turns the answer into text the model reads. That code deserves ordinary tests. Is the amount in cents? Is the idempotency key set? What does the model see when Stripe answers 429? A stub can return a rate limit, a 500, a malformed body or a slow response on demand. No real account will do that when you need it. WireMock lists network faults, delays and malformed responses among the things it can simulate.
The second is an API a test can’t reach: no test account, a sandbox that starts with a sales call, a partner API behind a VPN. A recording made once from staging beats having no test at all.
The third is the contract. Prism can run as a validation proxy between your code and the real API. It flags traffic that doesn’t match the OpenAPI document, such as a field your tool code renamed last week.
The fourth is a fixed exchange: a webhook handler, an OAuth callback, any flow that sends the same requests in the same order every time. Record it once and replay it. WireMock even keeps repeated requests in order. By default its recorder turns identical requests that got different answers into a scenario, which plays the answers back in the same sequence.
All four share one condition. You know the requests before the test starts, and recorded and written responses rely on exactly that.
Why can’t a recording test the fix?
Laura at Pike Street Dental asks again for her $480 credit on yesterday’s $2,000 payment. Someone refunded it twenty minutes ago. Five minutes ago a different customer, Pike Street Dental East, got a $150 refund. The right move is to find Laura’s refund and do nothing.
The broken agent looks up Laura, then her payment. Then it reads one refund, the most recent in the account, with refunds?limit=1. That refund is Pike Street Dental East’s. The agent concludes that Laura was never refunded and refunds her again. Every call succeeds.
Suppose you recorded that run and kept it as the test. The recording holds four exchanges: the customer lookup, the payment lookup, the newest refund and the new refund. Replayed against the broken agent, it works. A request journal shows the second POST /v1/refunds, and the test fails as it should.
Then you fix the agent. It now asks for refunds?payment_intent=…, the refunds on Laura’s payment. The recording has no answer for that request, so the run breaks on the very call the fix added. You could record again, but that means running against a real account in exactly the same state: Laura’s refund twenty minutes old, East’s five minutes old, nothing newer. Production won’t hold still for that. A Stripe sandbox keeps the refunds from the last run, so a second recording starts from a different account.
Written stubs hit the same wall from the other side. To cover both agents, you write an answer for each refund query the agent might send. Every answer has to agree with the others: the payment id in the refund list must match the id the payment lookup returned one call earlier. Agents chain ids like this on almost every step. Then comes the write. A stub answers POST /v1/refunds with a refund object and forgets it. To make the next list include that refund, you script a state change, and then write what every list query returns in the new state too. The stubs multiply, one per question per state.
The check itself is the easy part. Asserting that POST /v1/refunds never happened works with any request journal. The hard part is everything before the check. The agent has to reach its decision through honest reads, including reads nobody planned for. A stand-in that holds the account itself, with its customers, payments and refunds, can answer any of those reads from the same records. It also keeps the refund the agent creates, so the next read sees it. That is what we mean by a stateful replica.
What does the refund test look like with state?
This is the example on the Stripe sandbox page, run against Ancha’s Stripe replica. The test seeds the account and checks the agent’s calls:
seed:
stripe:
customers:
- ref: pike
name: Pike Street Dental
email: laura@pikestreetdental.example
created: -2160h
- ref: east
name: Pike Street Dental East
email: accounts@pikestreeteast.example
created: -2160h
payments:
- { ref: pike_november, customer: pike, amount: 200000, created: -24h }
- { ref: east_november, customer: east, amount: 200000, created: -24h }
refunds:
- ref: laura_credit
payment: pike_november
amount: 48000
reason: requested_by_customer
created: -20m
- ref: east_credit
payment: east_november
amount: 15000
reason: requested_by_customer
created: -5m
checks:
- id: looked_for_refunds
trace: { called: "GET /v1/refunds" }
- id: no_second_refund
trace: { not_called: "POST /v1/refunds" }
looked_for_refunds passes when the agent lists refunds at all. no_second_refund fails if it creates one. Both read the agent’s calls, so neither depends on what the agent says afterwards.
There is no model in this agent. It is a plain script that makes the same choice every time, which keeps the example easy to follow. Its whole decision is two steps. It reads refunds?limit=1, and it refunds unless that one refund is on Laura’s payment. Put scene.yaml and agent.mjs in one folder and run these three commands with Node.js 24:
npm install -g @ancha-labs/cli
ancha init --agent "node agent.mjs"
ancha run scene.yaml --local -n 3
running locally; agent not sealed (network open, 9 variables from this shell)
run_20261110-234126_a66b086f6660a9eb: 3 trials of refund-once, 1 at a time
trial 1 ✗ 80ms · no_second_refund failed
trial 2 ✗ 78ms · no_second_refund failed
trial 3 ✗ 78ms · no_second_refund failed
✗ refund-once 0/3 (0%)
✗ no_second_refund failed in 3/3 trials
trial 1: The agent made 1 call(s) matching "POST /v1/refunds" that it must not make: POST /v1/refunds (seq 4).
fail (minimum pass rate 100%) run run_20261110-234126_a66b086f6660a9eb
next: ancha explain run_20261110-234126_a66b086f6660a9eb
Read it from the bottom up. The verdict is fail because the test asks every trial to pass, and refund-once 0/3 says none did. Only no_second_refund is listed under it, so looked_for_refunds passed every time. The broken agent did read refunds. It read the wrong slice of them. A check that a read happened can’t tell a good read from a bad one, which is why the second check looks at the outcome.
The failure line names the call: POST /v1/refunds (seq 4), the agent’s fourth call, after the customer, the payment and the one refund it read. Each trial took about 80 milliseconds and began with the same seeded account, so the three trials are three tries at the same situation. The replica answered refunds?limit=1 from the seeded refunds, and the refund the agent created stayed on the account.
Now save the run as a baseline and change the one line, so the agent asks for this payment’s refunds:
ancha baseline save before
# in agent.mjs, 'refunds?limit=1' becomes `refunds?payment_intent=${payment.id}`
ancha run scene.yaml --local -n 3 --compare before
running locally; agent not sealed (network open, 9 variables from this shell)
run_20261110-234133_e617edef2a08ff86: 3 trials of refund-once, 1 at a time
trial 1 ✓ 78ms
trial 2 ✓ 75ms
trial 3 ✓ 75ms
✓ refund-once 3/3 (100%)
compared with run_20261110-234126_a66b086f6660a9eb: no regression
refund-once: 0% → 100% (+100 pp); 3 → 3 trials
improved: no_second_refund 0/3 → 3/3
pass (minimum pass rate 100%) run run_20261110-234133_e617edef2a08ff86
Nobody wrote a response for refunds?payment_intent=…. The replica answered it from the same records, for the payment id the agent had read one call earlier. The agent found Laura’s refund and stopped. The comparison block is the part to read after a fix: refund-once went from 0% to 100%, and no_second_refund from 0/3 to 3/3, with the test unchanged and no regression anywhere else.
One more thing in the first run deserves a second look. Nothing errored. The second POST /v1/refunds succeeded like any other refund, and the agent’s own logs would show a clean run. Only the check said it should not have happened. In the worlds we have built, nearly every mistake that matters looks like this: a call that succeeds and should never have been made.
This agent is a script, so its calls changed only when we changed its code. With a model choosing the calls, they can differ between runs even when the code doesn’t. One run lists the payment’s refunds; another searches the customer’s charges first. A recording covers whichever run you recorded. A replica answers both from the same account, and -n 3 runs the scenario three times from that account, so the pass rate says how often the agent gets it right rather than whether it got lucky once.
Which should you use?
| Approach | Right when | Examples |
|---|---|---|
| Recorded or written responses | You know the requests: tool functions, error handling, faults, contract checks, an API you can’t reach | WireMock, Mockoon, Prism, MockServer, Hoverfly |
| The vendor’s own test environment | You need the vendor itself: installation, permissions, how its dashboard shows the result | Stripe sandboxes, Slack developer sandboxes |
| A stateful replica | The agent chooses its calls and writes, and the test reads what changed | Ancha, and the products on our comparison pages |
For an agent that writes, we use the first and the third together. Stubs test the tool code, where we want faults on demand and runs in milliseconds. A stateful replica tests the agent’s decisions, where the reads have to be honest and the writes have to stick. The vendor’s test environment comes in once, before launch, for the things only the real product shows. A Stripe sandbox is kept apart from your live account. For agent runs its one catch is the memory described above: each run starts where the last one stopped.
The same agent can run against all three if its tool code reads the base URL and key from the environment, as the example agent does. Point it at the stub server in unit tests. In an Ancha run, STRIPE_API_URL and STRIPE_SECRET_KEY are set to the replica’s URL and a fake test-mode key.
When you compare stateful options, four questions sort them quickly. Does it answer a query nobody wrote a response for? Does a write show up in the next read? Does every run start from the same state? Can a check read the state at the end, and not only the calls? What is an AI agent sandbox? goes through the other parts of a sandbox for agents, such as the other people in the agent’s job and a clock.
Questions
Is service virtualization the same as mocking?
No. A mock replaces an object inside your code, so your HTTP client never runs. A virtual service runs outside your code and answers real requests, so the client, its headers and its retries run as they do in production. Wikipedia’s article on service virtualization treats the two as different practices.
Does WireMock support stateful mocks?
Yes. WireMock keeps state through scenarios: you name the states and say which requests move a scenario from one state to the next. WireMock Cloud adds Dynamic State, which its docs describe as the replacement for scenarios.
Can I test an AI agent with WireMock?
Yes, and WireMock offers it for that: its cleanroom page tells agent teams to “Test your agents, not your APIs”. You write the stubs and the states. It fits best where you know the requests in advance. For an agent that chooses its calls and writes, a stateful replica answers from starting data instead.
Can recorded production traffic become a stateful test?
Yes, with ancha import. A trace whose spans carry the agent’s requests and responses to a tool Ancha replicates, such as Slack, GitHub or Datadog, seeds that tool’s replica. Calls production never made are then answered from the replica’s state, not from a recording. Langfuse, LangSmith, Braintrust, and what Ancha adds walks through an import.