Skip to content

Instantly share code, notes, and snippets.

@RunWith(MockitoJUnitRunner.class)
public class VoucherDiscountCalculationTest {
private static final String NO_VOUCHER = null;
private static final String TWENTY_POUNDS_VOUCHER = "20";
@Mock
private VoucherService voucherService;
private VoucherDiscountCalculation voucherDiscountCalculation;
@RunWith(MockitoJUnitRunner.class)
public class VoucherPricingServiceTest {
private static final User UNUSED_USER = null;
private static final String NO_VOUCHER = null;
private static final String TWENTY_POUNDS_VOUCHER = "20";
@Mock private VoucherService voucherService;
private TestableVoucherPricingService voucherPricingService;
public class VoucherService {
public double getVoucherValue(String voucher) {
// Imagine that this calculate the voucher price.
// Keeping it simple so we can understand the approach.
return 0;
}
}
public class VoucherPricingService extends UserDiscountPricingService {
private VoucherService voucherService;
@Override
protected double applyAdditionalDiscounts(double total, User user, String voucher) {
double voucherValue = voucherService.getVoucherValue(voucher);
double totalAfterValue = total - voucherValue;
return (totalAfterValue > 0) ? totalAfterValue : 0;
}
public class ShoppingBasketBuilder {
private Map<Product, Integer> products = new HashMap<Product, Integer>();
public static ShoppingBasketBuilder aShoppingBasket() {
return new ShoppingBasketBuilder();
}
public ShoppingBasketBuilder with(Product product) {
this.products.put(product, 1);
public class ProductBuilder {
private double price;
private String name;
public static ProductBuilder aProduct() {
return new ProductBuilder();
}
public ProductBuilder costing (double price) {
@RunWith(MockitoJUnitRunner.class)
public class PricingServiceTest {
private static final String NO_VOUCHER = "";
private TestablePricingService pricingService = new TestablePricingService();
private ShoppingBasket shoppingBasket;
@Mock private PriceCalculation priceCalculation;
public abstract class PricingService {
private PriceCalculation priceCalculation;
public double calculatePrice(ShoppingBasket shoppingBasket, User user, String voucher) {
double discount = calculateDiscount(user);
double total = 0;
for (ShoppingBasket.Item item : shoppingBasket.items()) {
total += priceCalculation.calculateProductPrice(item.getProduct(), item.getQuantity());
}
public interface PriceCalculation {
double calculateProductPrice(Product product, int quantity);
}
public class BoxingDayPriceCalculation implements PriceCalculation
public class StandardPriceCalculation implements PriceCalculation
public class BoxingDayPriceCalculationTest {
private BoxingDayPriceCalculation priceCalculation = new BoxingDayPriceCalculation();
@Test public void
should_apply_boxing_day_discount_on_product_price() {
Product book = aProduct().costing(10).build();
double price = priceCalculation.calculateProductPrice(book, 1);