Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Writing Test Cases Without Mocks

HardExecution00:00
Practice interviewer
In session
5 left
00:00

Your question is Writing Test Cases Without Mocks. Take a moment with it on the right.

Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).

You need to log in / sign up to chat or submit.

Problem

You're interviewing for a Software Engineer role at Zemoso Technologies, working on a client's order pricing service. The interviewer hands you the classes below and explains that the pricing logic calls static utility methods that can throw exceptions on bad input.

public class OrderPricingService {

    public double calculateFinalPrice(Order order) {
        double basePrice = order.getBasePrice();
        double tax = TaxCalculator.calculate(order.getRegion(), basePrice);
        double converted = CurrencyConverter.convert(basePrice + tax, order.getCurrency());
        return converted;
    }
}

public class TaxCalculator {
    public static double calculate(String region, double amount) {
        if (region == null) {
            throw new IllegalArgumentException("Region is required");
        }
        double rate = TaxRates.getRate(region);
        return amount * rate;
    }
}

public class CurrencyConverter {
    public static double convert(double amount, String currency) {
        if (!ExchangeRates.isSupported(currency)) {
            throw new UnsupportedCurrencyException(currency);
        }
        return amount * ExchangeRates.getRate(currency);
    }
}

How would you write test cases that cover both the happy path and the exception paths of calculateFinalPrice, without using Mockito or PowerMock, and without changing the logic inside TaxCalculator or CurrencyConverter? You may refactor as long as behavior stays the same. Explain your approach in words.