ExchangeRatesServiceImpl implements ExchangeRatesService { @Override public BigDecimal convert(Currency from, Currency to, BigDecimal amount) { Assert.notNull(amount); Map<Currency, BigDecimal> rates = getCurrentRates(); BigDecimal ratio = rates.get(to).divide(rates.get(from), 4, RoundingMode.HALF_UP); return amount.multiply(ratio); }  @Override Map<Currency, BigDecimal> getCurrentRates(); @Override BigDecimal convert(Currency from, Currency to, BigDecimal amount);  }
@Test public void shouldConvertCurrency() { ExchangeRatesContainer container = new ExchangeRatesContainer(); container.setRates(ImmutableMap.of( Currency.EUR.name(), new BigDecimal("0.8"), Currency.RUB.name(), new BigDecimal("80") )); when(client.getRates(Currency.getBase())).thenReturn(container); final BigDecimal amount = new BigDecimal(100); final BigDecimal expectedConvertionResult = new BigDecimal("1.25"); BigDecimal result = ratesService.convert(Currency.RUB, Currency.USD, amount); assertTrue(expectedConvertionResult.compareTo(result) == 0); }
@Test(expected = IllegalArgumentException.class) public void shouldFailToConvertWhenAmountIsNull() { ratesService.convert(Currency.EUR, Currency.RUB, null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void shouldFailToConvertWhenAmountIsNull() { ratesService.convert(Currency.EUR, Currency.RUB, null); }