/** test of iva method of class CalculateIva */ @Test public void testCalculateIva() { CalculateIva calculateIva = Mockito.mock(CalculateIva.class); Purchase p = BillingCalculator.calculateTotalPurchase(customer, "EL-003,FU-001"); when(CalculateIva.iva(p.getCode())).thenReturn(new BigDecimal(Math.random() * 0.05)); }
@Test public void testPay() { GenericCreditCardService creditCardService = Mockito.mock(GenericCreditCardService.class); Purchase p = BillingCalculator.calculateTotalPurchase(customer, "EL-001,FU-002"); // Mocking external service behavior Mockito.when(creditCardService.pay("xxxx111xxxx", p.getTotalPrice())).thenReturn(true); paymentService.pay(customer, p, "xxxx111xxxx", creditCardService); }
/** test for all methods of customer points system of class Customer */ @Test public void addPoints() { int points; String productsFlatFile = "EL-002,FU-007,FU-008"; Purchase result = null; result = BillingCalculator.calculateTotalPurchase(customer, productsFlatFile); Customer client = Mockito.mock(Customer.class); when(client.calculatePoints()) .thenReturn(points = (result.getTotalPrice().divide(new BigDecimal(1000))).intValue()); when(client.addPoints()).thenReturn(customer.getPoints().add(new BigDecimal(points))); customer.calculatePoints(); customer.addPoints(); }
@Test public void calculateDiscountPoints() { String productsFlatFile = "EL-002,FU-007,FU-008"; Purchase result = null; result = BillingCalculator.calculateTotalPurchase(customer, productsFlatFile); Customer client = Mockito.mock(Customer.class); client = result.getCustomer(); when(client.calculateDiscountPoints()) .thenReturn(result.getTotalPrice().multiply(new BigDecimal(Math.random() * 0.02))); when(client.discountPoints()).thenReturn(true); if ((client.getPoints().intValue()) > 1000) { client.discountPoints(); } }
/** Test of calculateTotalPurchase method, of class BillingCalculator. */ @Test public void testCalculateTotalPurchase() { String productsFlatFile = "EL-002,FU-007,FU-008"; Purchase result = null; BigDecimal discounts = BigDecimal.ZERO; BigDecimal price = BigDecimal.ZERO; result = BillingCalculator.calculateTotalPurchase(customer, productsFlatFile); for (String code : productsFlatFile.split(",")) { Product p = Product.buildProduct(code); if (code.startsWith("EL")) { // true values price = price.add(p.getPrice()); // just discounts discounts = discounts.add(p.getPrice().multiply(new BigDecimal(0.05))); } else if (code.startsWith("FU")) { price = price.add(p.getPrice()); discounts = discounts.add(p.getPrice().multiply(new BigDecimal(0.1))); } } assertEquals(result.getTotalPrice().add(discounts), price); }