From e1d8885bd48d4002cc2a9ff3e27883ca8f8e0952 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 4 Dec 2016 17:16:15 +0100 Subject: [PATCH] Delegating the responsability of storing transactions. TransactionRepository will store transactions. --- .../main/java/org/craftedsw/feature/Account.java | 8 ++++++++ .../java/org/craftedsw/feature/AccountShould.java | 21 +++++++++++++++++++-- .../craftedsw/feature/PrintStatementFeature.java | 5 ++++- .../craftedsw/feature/TransactionRepository.java | 4 ++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Account.java b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Account.java index fb43a95..a24744b 100644 --- a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Account.java +++ b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Account.java @@ -2,10 +2,18 @@ package org.craftedsw.feature; 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() { diff --git a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/AccountShould.java b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/AccountShould.java index 9b2c441..07d0c32 100644 --- a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/AccountShould.java +++ b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/AccountShould.java @@ -2,17 +2,34 @@ package org.craftedsw.feature; 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); + } } diff --git a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/PrintStatementFeature.java b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/PrintStatementFeature.java index c2c164a..ee1bfbb 100644 --- a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/PrintStatementFeature.java +++ b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/PrintStatementFeature.java @@ -17,7 +17,10 @@ public class PrintStatementFeature { @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 diff --git a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepository.java b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepository.java index a8a86f1..08e3ef7 100644 --- a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepository.java +++ b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepository.java @@ -6,4 +6,8 @@ public class TransactionRepository { throw new UnsupportedOperationException(); } + public void addWithdrawal(int amount) { + throw new UnsupportedOperationException(); + } + } -- 2.1.4