public class Account {
+ private TransactionRepository transactionRepository;
+
+ public Account(TransactionRepository transactionRepository) {
+ this.transactionRepository = transactionRepository;
+ }
+
public void deposit(int amount) {
+ transactionRepository.addDeposit(amount);
}
public void withdraw(int amount) {
+ transactionRepository.addWithdrawal(amount);
}
public void printStatement() {
import static org.mockito.Mockito.verify;
+import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+@RunWith(MockitoJUnitRunner.class)
public class AccountShould {
- private TransactionRepository transactionRepository;
+ @Mock private TransactionRepository transactionRepository;
+ private Account account;
+ @Before
+ public void initialise() {
+ account = new Account(transactionRepository);
+ }
+
@Test public void
store_a_deposit_transaction() {
-
+ account.deposit(100);
verify(transactionRepository).addDeposit(100);
}
+ @Test public void
+ store_a_withdrawal_transaction() {
+ account.withdraw(100);
+
+ verify(transactionRepository).addWithdrawal(100);
+ }
}
@Before
public void initialise() {
- account = new Account();
+ // Acceptance test is using the real repository because the acceptance test
+ // is testing the system as a whole. We are just mocking the external world (the Console)
+ TransactionRepository transactionRepository = new TransactionRepository();
+ account = new Account(transactionRepository);
}
@Test public void