package cz.fi.muni.pa165; import java.math.BigDecimal; import java.util.Currency; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import static org.mockito.Mockito.*; import static org.junit.Assert.*; interface ExchangeRateTable { BigDecimal getExchangeRate(Currency sourceCurency, Currency targCurrency); } class CurrencyConvertor { private final ExchangeRateTable exchangeRateTable; public CurrencyConvertor(ExchangeRateTable exchangeRateTable) { this.exchangeRateTable = exchangeRateTable; } public BigDecimal convert(Currency sourceCurency, Currency targCurrency, BigDecimal amount) { throw new UnsupportedOperationException(); } } @RunWith(MockitoJUnitRunner.class) public class ExampleTest { @Rule public final ExpectedException expectedException = ExpectedException.none(); @Mock private ExchangeRateTable exchangeRateTable; private CurrencyConvertor currencyConvertor; private final static Currency CZK = Currency.getInstance("CZK"); private final static Currency EUR = Currency.getInstance("EUR"); @Before public void init() { currencyConvertor = new CurrencyConvertor(exchangeRateTable); } @Test(expected = IllegalArgumentException.class) public void testNullArgument() { currencyConvertor.convert(null, CZK, BigDecimal.ONE); } @Test public void testNullArgument2() { // test initialization expectedException.expect(IllegalArgumentException.class); currencyConvertor.convert(null, CZK, BigDecimal.ONE); } @Test public void testSomeMethod() { when(exchangeRateTable.getExchangeRate(EUR, CZK)) .thenReturn(new BigDecimal("25.38")); BigDecimal result = currencyConvertor.convert(EUR, CZK, BigDecimal.ONE); assertEquals(new BigDecimal("25.38"), result); } @Test public void testExceptionThrown() { when(exchangeRateTable.getExchangeRate(EUR, CZK)) .thenThrow(new RuntimeException()); currencyConvertor.convert(EUR, CZK, BigDecimal.ONE); } }