package ex07; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import static org.mockito.Mockito.*; import org.junit.Test; import org.mockito.Mockito; import common.BankService; import common.Money; import common.TransferService; @SuppressWarnings("unused") public class TransferServiceTestMockito { @Test public void transferWithCurrencyConversion() { BankService bankService = mock(BankService.class); when(bankService.getAccountCurrency("123_EEK")).thenReturn("EEK"); when(bankService.getAccountCurrency("456_EUR")).thenReturn("EUR"); when(bankService.convert(new Money(15, "EEK"), "EEK")).thenReturn(new Money(1, "EUR")); when(bankService.hasSufficientFundsFor(new Money(1, "EUR"))).thenReturn(true); TransferService transferService = new TransferService(bankService); transferService.transfer(new Money(15, "EEK"), "123_EEK", "456_EUR"); verify(bankService).convert(new Money(15, "EEK"), "EEK"); verify(bankService).credit(new Money(1, "EUR"), "123_EEK"); verify(bankService).debit(anyMoney(), anyString()); // verify(bankService).debit(... } private Money anyMoney() { return (Money) anyObject(); } @Test public void transferWithNoMoney(){ BankService bankService = mock(BankService.class); when(bankService.hasSufficientFundsFor(anyMoney())).thenReturn(false); verify(bankService, never()).credit(anyMoney(), anyString()); } }