-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathPaymentBrokerTest.java
67 lines (53 loc) · 1.94 KB
/
PaymentBrokerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package workshop.payment;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class PaymentBrokerTest {
private WalletInterface wallet;
private PaymentProviderInterface provider;
private PaymentBroker broker;
@Before
public void setUp() {
// Arrange
wallet = mock(WalletInterface.class);
provider = mock(PaymentProviderInterface.class);
broker = new PaymentBroker(wallet, provider);
}
@Test
public void testPay_WalletHasFundsAndProviderIsAvailableAndDepositSucceeded_ShouldReturnTrue() throws InsufficientFundsException, ProviderNotAvailableException {
// Arrange
int amount = 10;
int balance = 20;
when(wallet.getBalance()).thenReturn(balance);
when(provider.isAvailable()).thenReturn(true);
when(provider.deposit(wallet.getId(), amount)).thenReturn(true);
// Act & Assert
Assert.assertTrue(broker.pay(amount));
}
// Assert
@Test(expected = InsufficientFundsException.class)
public void testPay_WalletDoesNotHaveFunds_ShouldThrowInsufficientFundsException() throws InsufficientFundsException, ProviderNotAvailableException {
// Arrange
int amount = 10;
int balance = 9;
when(wallet.getBalance()).thenReturn(balance);
when(provider.isAvailable()).thenReturn(true);
when(provider.deposit(wallet.getId(), amount)).thenReturn(true);
// Act
broker.pay(amount);
}
// Assert
@Test(expected = ProviderNotAvailableException.class)
public void testPay_ProviderIsNotAvailable_ShouldThrowProviderNotAvailableException() throws InsufficientFundsException, ProviderNotAvailableException {
// Arrange
int amount = 10;
int balance = 20;
when(wallet.getBalance()).thenReturn(balance);
when(provider.isAvailable()).thenReturn(false);
when(provider.deposit(wallet.getId(), amount)).thenReturn(true);
// Act
broker.pay(amount);
}
}