🔒 Hello, this is Valentina with a premium issue of the Optivem Journal. I help Engineering Leaders & Senior Software Developers cover legacy code with acceptance tests, using AI. This article is part of the Acceptance Testing in Legacy Code with AI series. To get these articles in your inbox every week, subscribe.
We have a legacy system with no acceptance tests, and we want to cover it using AI. In Acceptance Criteria for the Core Use Case in Legacy Code with AI, we wrote the acceptance criteria for Place Order and the use cases it depends on (Create Admin, Create Customer, Publish Coupon and View Order).
Now we want to turn the acceptance criteria into acceptance tests. But not so fast! Acceptance tests rely on stubs for the external systems: the ERP, Tax and the Clock. If a stub is wrong, we waste time troubleshooting a failing acceptance test, not knowing whether the problem is the stub or the acceptance test. So first we build the stubs, and contract tests that check them against the real systems.
Matching means matching the contract: the stub takes the same calls as the real external system and gives back the same answers, so the same contract tests pass on both. That makes the stubs trusted, and the acceptance tests use the trusted stubs: a failing acceptance test is never the stub.
Adopt Acceptance Tests in Legacy Code with AI in 2027
AI writes code faster than anyone can test it. Acceptance tests are how your teams keep up, especially in legacy code: the agent writes them, the human approves them, the pipeline enforces them, and every feature gets verified before release. I help you adopt acceptance testing: I set up the test architecture and cover your existing legacy product with acceptance tests. I then coach your teams to apply ATDD to their user stories and bug fixes. If you want to make this happen in 2027, book a call.
Legacy Code Already Has the Integration
In legacy code, the integration with the external systems already exists. We’re not designing anything, we’re mapping what’s already there.
Only Place Order calls external systems, and it calls all three: the ERP for the product and the promotion, Tax for the tax rate, the Clock for the time.
The steps:
List the stub operations from the Givens
Find the call behind each stub operation
Make the external systems swappable
Write the contract tests
Build the stubs
The prompts work for any use case: replace <use case> with its name, like Place Order (UC-001), and <use-case> with its file name, like UC-001-place-order. The examples show Place Order, in an online shop.
1. List the Stub Operations from the Givens
We don’t read the source code again to decide what to stub: the Givens already say it. In Acceptance Criteria for the Core Use Case in Legacy Code with AI, behind each Given was either one of our use cases or an answer from an external system. Every answer from an external system is a stub operation.
AI prompt: List the stub operations
<use case>needs, indocs/stubs/<use-case>.md.
Read the acceptance criteria in
docs/acceptance-criteria/<use-case>.md.For each Given that is an answer from an external system, write one stub operation: what the external system returns, and for which input.
Group the stub operations by external system. Under each one, quote the Givens that need it.
Leave out the Givens that run our own use cases: those run for real, never stubbed.
The Givens come from the acceptance criteria of Place Order, like these (trimmed here):
@BR-5
Scenario: Tax uses the tax rate of the customer's country
Given a product "ABC-123" with unit price 20.00
And no promotion is active
And a customer from the US
And the tax rate for the US is 0.10
When the customer orders 5 of "ABC-123"
Then the order is placed successfully
And the tax is 10.00
@narrative-3a
Scenario: Order for a product that doesn't exist fails
Given there is no product "XYZ-999"
When the customer orders 1 of "XYZ-999"
Then placing the order fails
And the customer is told "Product does not exist for SKU: XYZ-999"
@narrative-3b
Scenario: Order when the ERP is unavailable fails
Given the ERP is unavailable
When the customer places an order
Then placing the order fails
And the customer is told "Product information can't be retrieved right now"
@narrative-2a
Scenario: Order during year-end closing fails
Given the time is 2026-12-31T23:59:30Z
When the customer places an order
Then placing the order fails
And the customer is told "Orders cannot be placed between 23:59 and 00:00 on December 31st"
# ...
Every Given here is an answer from the ERP, Tax or the Clock, except “a customer from the US”: that one runs Create Customer, so it isn’t stubbed.
For Place Order, the output from AI may look like this, in docs/stubs/UC-001-place-order.md:
## ERP
- Returns a product for a SKU: "a product "ABC-123" with unit price 20.00"
- Has no product for a SKU: "there is no product "XYZ-999""
- Returns whether a promotion is active: "no promotion is active"
- Is unavailable: "the ERP is unavailable"
## Tax
- Returns the tax rate for a country: "the tax rate for the US is 0.10"
- Has no tax rate for a country: "there is no tax rate for "ZZ""
## Clock
- Returns the time: "the time is 2026-12-31T23:59:30Z"
That’s the whole list. A stub operation that no Given needs doesn’t get built.




