Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

You must change pricing logic in a 3,000-line legacy class with no tests and static calls to the database. How do you start?

I would not start by changing the pricing logic. I would first write characterisation tests that record what the class does today, and to make it testable I would find seams: places where I can replace the static database calls, by wrapping them behind an interface or a method I can override in a test. For complex outputs I would use approval or snapshot tests, then refactor in small steps, running the tests after each one. Mocking the statics directly is acceptable as a temporary step, but the end state is a class that receives its dependencies.

Why this question is asked

Large legacy classes with static database access are common in long-lived Java systems, and changing them is where many production incidents begin. Interviewers ask this scenario to see whether we have a method for working safely in code we do not fully understand, rather than either rewriting it or changing it blind. In this answer, we will look at why the static calls are the real obstacle, how to find and create seams, how characterisation and approval tests pin down behaviour, how to refactor in small steps, and where mocking statics fits.

1Why the Static Calls Are the Real Obstacle

A 3,000-line class with no tests is hard to understand, but the bigger problem is that we cannot run it in a test. Suppose the pricing method calls Db.findCustomer(id) and Db.loadRates(region) directly. Those are static methods, so there is no object we can swap for a fake. Every call goes to a real database, which means a test would need a populated database, would be slow, and would give different results as the data changes.

So the first job is not to understand all 3,000 lines. It is to get the one method we must change running in a test, with controlled inputs. Michael Feathers' term for the place where we can change behaviour without editing the code at that point is a seam, and finding seams is the heart of legacy testing.

Java
class PriceCalculator { // 3,000 lines
public BigDecimal quote(long customerId, String region, List<Line> lines) {
Customer c = Db.findCustomer(customerId); // static: real database
RateTable rates = Db.loadRates(region); // static: real database
// ... 400 lines of pricing rules ...
}
}

2Creating Seams Without Changing Behaviour

The safest first seam needs almost no design work. We wrap each static call in a small protected method in the same class, and the rest of the code calls that method instead. This is a mechanical refactoring that IDEs can do, and it does not change behaviour.

Java
class PriceCalculator {
public BigDecimal quote(long customerId, String region, List<Line> lines) {
Customer c = findCustomer(customerId);
RateTable rates = loadRates(region);
// ... unchanged pricing rules ...
}
protected Customer findCustomer(long id) { return Db.findCustomer(id); }
protected RateTable loadRates(String region) { return Db.loadRates(region); }
}
// In the test: a subclass overrides the seams
class TestablePriceCalculator extends PriceCalculator {
Customer customer; RateTable rates;
@Override protected Customer findCustomer(long id) { return customer; }
@Override protected RateTable loadRates(String region) { return rates; }
}

Now the test can create a TestablePriceCalculator, set a customer and a rate table, and call quote() without a database. This technique is sometimes called subclass and override. It is not the final design, but it gets the code under test today with a change small enough to review in a minute.

The better seam, once we have some tests, is an interface: a CustomerRepository and a RateRepository passed in through the constructor, with the old static calls inside the production implementations. That lets the class be built with fakes in tests and with real repositories by Spring in production.

3Recording Current Behaviour

With the seams in place, we write characterisation tests: we call the method with realistic inputs, look at what it returns, and assert exactly that. We are not checking that the result is right, only that it stays the same while we work. Good inputs come from production: a few anonymised real quotes, plus the cases the pricing rules obviously branch on, such as each region, each customer type and quantities around any thresholds we see in the code.

For a method that returns a complex object, such as a quote with 20 lines, taxes and discounts, writing assertions for every field is slow. Approval tests, also called snapshot tests, help here. The test converts the result into readable text, and the library compares it with a stored, approved file. The first run produces a received file that we read and approve; later runs fail if the output changes, and show a diff.

Java
@Test void quoteForWholesaleCustomerInEu() {
calc.customer = customer("WHOLESALE", "DE");
calc.rates = rates("EU-2026-Q3");
Quote q = calc.quote(42L, "EU", lines("A:12", "B:3"));
Approvals.verify(q.toReport()); // compared with ...quoteForWholesaleCustomerInEu.approved.txt
}

The approved files live in version control next to the tests, so a reviewer sees exactly which outputs a change affected. We aim for perhaps 20 to 40 of these cases around the method we must change, guided by a coverage report, so that each branch of the pricing rules is exercised at least once.

4Refactoring in Small Steps

Now we can change code. The rule is small steps with the tests run after every step: extract a method, rename a variable, move the discount rules into their own class, replace a static call with an injected repository. Each step should take minutes, and if the tests fail we undo it rather than debug a large change.

Only when the area we need is in reasonable shape do we make the actual pricing change. We write a test for the new behaviour first and watch it fail, then make it pass. The characterisation tests show us whether we changed anything else. Where the new rule intentionally changes old outputs, we update the approved files in the same commit, so the review shows the before and after.

Step throughChanging a legacy class safely
  1. Create seamswrap static calls in overridable methods
  2. Write characterisation testsapproval tests for complex outputs
  3. Refactor one small step
  4. Tests still green?
    YesCommit and take the next stepNoUndo the step
  5. Write a failing test for the change
  6. Make the pricing changeupdate approved files only where intended

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

Every step is small enough to undo; the tests decide whether we keep it.

5Where Mocking Statics Fits

Mockito can mock static methods with mockStatic(), and since Mockito 5 the inline mock maker that supports it is the default. That is useful when even the subclass-and-override change is too risky to make before having any test at all.

Java
try (MockedStatic<Db> db = Mockito.mockStatic(Db.class)) {
db.when(() -> Db.findCustomer(42L)).thenReturn(customer("WHOLESALE", "DE"));
db.when(() -> Db.loadRates("EU")).thenReturn(rates("EU-2026-Q3"));
Approvals.verify(new PriceCalculator().quote(42L, "EU", lines("A:12")).toReport());
} // the static mock is removed when the block closes

But it should be a temporary step. Static mocks hide the dependency instead of fixing it, they apply to the whole thread for the duration of the block, and tests that use them stay tied to the internal calls. Once the characterisation tests exist, we refactor towards injected dependencies and delete the static mocks. A good sign of progress is that the number of mockStatic calls in the test suite goes down over time.

6Follow-up questions

Would you rewrite the class instead?

Not as a first step. A rewrite of 3,000 lines of pricing logic almost always loses rules nobody remembered, and it delays the change the business is waiting for. With characterisation tests in place, we can extract and rewrite parts gradually, which keeps the system working all the time.

How do you choose inputs for characterisation tests?

Start from the branches in the code near the change, add a coverage report to see which ones the tests reach, and add a few anonymised real cases from production logs or the database. Inputs near thresholds, such as quantities or dates around a rule change, are especially valuable.

What if the current behaviour is clearly wrong?

Record it anyway, mark the test with a comment, and raise it with the product owner. Fixing it should be its own deliberate change with its own test, so it is not mixed into the pricing change and does not surprise anyone who depends on the current result.

7Conclusion

In this answer, we saw how to start on a large untested class with static database calls. We create seams, first by wrapping static calls in overridable methods and later with injected interfaces, so the code can run in a test. We record current behaviour with characterisation tests and approval tests for complex outputs, refactor in small steps with the tests run after each one, and only then make the pricing change, test first. Mocking statics can get us started but should disappear as the design improves.

What to say in the interviewAbout 90 seconds aloud

Open with

My first goal would be to get the method I need to change running in a test, without changing what it does. The static database calls are the real obstacle, because there is no object to replace.

Then, if they want more

So I would create seams. The quickest and safest is to wrap each static call in a small protected method in the same class, which is a mechanical refactoring, and then in the test use a subclass that overrides those methods and returns prepared data. Later I would move to proper interfaces passed in through the constructor.

With that in place, I would write characterisation tests, which record what the code does today rather than what it should do. For complex outputs like a whole quote, I would use approval tests, which store the output as a text file that I review once and that fails with a diff if anything changes.

I would pick inputs from the branches near my change and a few anonymised real cases. Then I refactor in small steps, running the tests after each one and undoing anything that breaks. Only then do I make the pricing change, with a failing test first.

If even the wrapping feels risky, I can use Mockito's mockStatic to get started, but that is temporary; the end state is injected dependencies.

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 "You must change pricing logic in a 3,000-line legacy class with no tests and static...": https://scrivio-interview-library.netlify.app/java/test-strategy/3925c2a639/