### Question:
PricingServiceBean implements PricingService { public Price discountedPrice(Product product, Account account) { try { DiscountService discounts = getDiscountService(); int discount = discounts.getDiscountPercentage(account); float discountMultiplier = ((100 - discount) / 100.0f); return new Price((int) (product.getPrice() * discountMultiplier)); } catch (Exception e) { throw new RuntimeException(e); } }  @EJB(beanInterface = DiscountService.class) void setDiscountService(DiscountService impl); @PostConstruct void ejbCreate(); Price discountedPrice(Product product, Account account);  }### Answer:
@Test public void discountBasedOnVolumeOfPastPurchases1() throws Exception { Account account = new MockAccount(); Product product = new Product("PROD001", 10000); DiscountService discountService = createMock(DiscountService.class); expect(discountService.getDiscountPercentage(account)) .andReturn(25); replay(discountService); InitialContext context = new InitialContext(); context.bind("example/DiscountServiceBean/local", discountService); PricingServiceBean bean = new PricingServiceBean(); Price price = bean.discountedPrice(product, account); verify(discountService); assertEquals(new Price(7500), price); }
@Test public void discountBasedOnVolumeOfPastPurchases2() throws Exception { Account account = new MockAccount(); Product product = new Product("PROD001", 10000); final DiscountService discountService = createMock(DiscountService.class); expect(discountService.getDiscountPercentage(account)) .andReturn(25); replay(discountService); PricingServiceBean bean = new PricingServiceBean() { @Override protected DiscountService getDiscountService() { return discountService; } }; Price price = bean.discountedPrice(product, account); verify(discountService); assertEquals(new Price(7500), price); }
@Test public void discountBasedOnVolumeOfPastPurchases3() throws Exception { Account account = new MockAccount(); Product product = new Product("PROD001", 10000); DiscountService discountService = createMock(DiscountService.class); expect(discountService.getDiscountPercentage(account)) .andReturn(25); replay(discountService); PricingServiceBean bean = new PricingServiceBean(); injectField(bean, "discountService", discountService); Price price = bean.discountedPrice(product, account); verify(discountService); assertEquals(new Price(7500), price); }