Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

You need to add a feature to this method with no tests:

Java
double price(Order o) {
if (o.items().size() > 10) return o.total() * 0.9;
if (o.customer().isVip()) return o.total() * 0.95;
return o.total();
}

Which tests would you write first, and why?

Before touching the method, I would write characterisation tests that pin down what it does today: more than 10 items, exactly 10 items, a VIP with few items, a VIP with many items, and a normal customer. The boundary at 10 and the order of the two checks, where the bulk discount wins over the VIP discount, are the easiest things to break. Then I would add the new feature with its own failing test first, and separately raise that double is unsafe for money and should become BigDecimal or integer cents.

Why this question is asked

Changing a method that has no tests is one of the most common tasks in real work, and also one of the riskiest. The method looks simple, which is exactly why people change it without a safety net and break a rule nobody wrote down. Interviewers use this question to see whether we protect existing behaviour first, whether we notice boundaries and ordering, and whether we spot the money problem. In this answer, we will recap what characterisation tests are, read the method carefully, write the tests in the order I would write them, add the new feature test first, and look at the double problem.

1A Quick Recap of Characterisation Tests

A characterisation test records what the code does now, not what we think it should do. We call the code with an input, observe the result, and write a test that expects exactly that result. If the current behaviour turns out to be surprising, we still record it first and ask the business later, because other systems or customers may depend on it.

The purpose is a safety net. Once the current behaviour is pinned down, any change we make that alters it will fail a test, and we will know immediately whether the change was intended. Without these tests, we only find out from a customer.

2Reading the Method Carefully

Here is the method, laid out on several lines.

Java
double price(Order o) {
if (o.items().size() > 10) return o.total() * 0.9; // bulk: 10% off, checked first
if (o.customer().isVip()) return o.total() * 0.95; // VIP: 5% off
return o.total(); // everyone else
}
Characterisation cases for price()
CaseItemsVIPTotalExpected now
Bulk order11no100.0090.00
Exactly 10 items10no100.00100.00
VIP, few items2yes100.0095.00
VIP, many items: bulk wins11yes100.0090.00
VIP, exactly 10 items10yes100.0095.00
Normal customer2no100.00100.00
The highlighted rows protect the boundary and the order of the checks.

Three facts matter. First, the bulk rule uses > 10, so 11 items get the discount and exactly 10 do not. That boundary is a classic place for an off-by-one change. Second, the checks are in order and each returns immediately, so a VIP with 11 items gets 10 percent, not 5 and not 14.5 combined. Discounts do not stack. Third, everything is double arithmetic, which we will come back to.

Those facts give us the cases. Each branch needs a test, and each boundary and the ordering need one too.

3Writing the Tests First

I would write these as small, named tests with a helper that builds orders, so each test reads as one rule. A total of 100.00 keeps the expected numbers obvious, and a small delta in assertEquals avoids failing on floating-point noise, since the method returns double.

Java
class PriceCharacterizationTest {
Pricing pricing = new Pricing();
Order order(int items, boolean vip) {
return new Order(Collections.nCopies(items, new Item("sku")), new Customer(vip), 100.00);
}
@Test void bulkDiscountAboveTenItems() { assertEquals(90.0, pricing.price(order(11, false)), 1e-9); }
@Test void noBulkDiscountAtExactlyTen() { assertEquals(100.0, pricing.price(order(10, false)), 1e-9); }
@Test void vipDiscountForSmallOrders() { assertEquals(95.0, pricing.price(order(2, true)), 1e-9); }
@Test void bulkWinsOverVip() { assertEquals(90.0, pricing.price(order(11, true)), 1e-9); }
@Test void vipAtExactlyTenGetsVipRate() { assertEquals(95.0, pricing.price(order(10, true)), 1e-9); }
@Test void normalCustomerPaysFullPrice() { assertEquals(100.0, pricing.price(order(2, false)), 1e-9); }
}

Notice the names. If someone later changes > 10 to >= 10, the test noBulkDiscountAtExactlyTen fails and its name explains the rule that broke. If someone reorders the checks, bulkWinsOverVip fails. That is the value of naming tests after behaviour rather than test1 and test2.

If building an Order is hard, because the real class needs a database or many collaborators, I would still find a way to construct one in the test, perhaps with a test builder, rather than skip these tests. The method itself only needs items(), customer() and total().

4Adding the New Feature Test First

With the current behaviour pinned down, we can add the feature. Suppose the new requirement is a 15 percent discount during a promotion week. I would write that test first and watch it fail, because a test we never saw fail might not be testing anything.

Java
@Test void promotionWeekGivesFifteenPercent() {
Pricing pricing = new Pricing(promotionActive(true));
assertEquals(85.0, pricing.price(order(2, false)), 1e-9); // fails before the change: 100.0
}

Writing it forces the questions the requirement left open: does the promotion stack with VIP? Does bulk still win? Each answer becomes another test, and the characterisation tests tell us whether we changed anything we did not mean to. Notice too that the promotion depends on a date; passing it in, or injecting a Clock, keeps the test deterministic instead of depending on the day it runs.

5The Separate Problem: double for Money

The last thing I would raise, as a separate change, is the use of double. A double is a binary floating-point number, and most decimal fractions such as 0.95 or 19.99 cannot be stored exactly in it.

Java
System.out.println(19.99 * 0.95); // 18.990499999999997, not 18.9905
System.out.println(0.1 + 0.2); // 0.30000000000000004
BigDecimal p = new BigDecimal("19.99").multiply(new BigDecimal("0.95"))
.setScale(2, RoundingMode.HALF_EVEN);
System.out.println(p); // 18.99

Small errors like these add up across many orders and lead to totals that differ from the bank by a cent. Money should be BigDecimal with an explicit scale and rounding mode, or a long count of cents. I would not mix this into the feature change, though. It changes the method's signature and its rounding, so it deserves its own change, with the characterisation tests updated deliberately to the new, correctly rounded values.

6Follow-up questions

Why not just refactor the method and test the new version?

Because without tests of the current behaviour, we cannot tell whether the refactoring changed anything. The boundary at 10 and the ordering of the checks are easy to lose when rewriting. Pinning the behaviour first turns a risky change into a safe one.

What if the current behaviour looks like a bug, such as discounts not stacking?

I would still record it as it is, and then ask the product owner. Customers or other systems may rely on it. If the business confirms it is a bug, we change the test deliberately as part of the fix, so the change in behaviour is visible in the review.

Would you use parameterised tests here?

A JUnit @ParameterizedTest with a @CsvSource of items, VIP flag and expected price would also work and is compact. I prefer separate named tests for the boundary and the ordering, because their names document the rules, and I might use a parameterised test for the remaining simple cases.

7Conclusion

In this answer, we saw how to change an untested method safely. We read it carefully, found the boundary at 10 items and the fact that the bulk discount wins over the VIP discount, and wrote characterisation tests for each branch, the boundary and the ordering. Then we added the new feature with its own failing test first, and raised the use of double for money as a separate change towards BigDecimal or integer cents.

What to say in the interviewAbout 90 seconds aloud

Open with

Before changing anything, I would write characterisation tests that record what the method does today, because there is no safety net and this kind of method has hidden rules.

Then, if they want more

Reading it, three things stand out. The bulk rule is greater than 10, so exactly 10 items get no bulk discount. The checks are in order and return immediately, so a VIP with eleven items gets the 10 percent bulk discount, not the VIP discount and not both. And it uses double for money.

So my first tests, with a total of 100 to keep the numbers obvious, would be: eleven items gives 90, exactly ten items gives 100, a VIP with two items gives 95, a VIP with eleven items gives 90, a VIP with exactly ten gives 95, and a normal customer gives 100.

I would name each test after the rule, like bulkWinsOverVip, so if someone changes the boundary or the order, the failing test explains what broke. Then I would add the new feature with its own test first and watch it fail, which also forces questions like whether the new discount stacks.

Separately, I would raise that double cannot represent amounts like 0.95 exactly, so money should move to BigDecimal with explicit rounding or to cents, as its own change.

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 need to add a feature to this method with no tests:": https://scrivio-interview-library.netlify.app/java/test-strategy/9e27e096bc/