Delegating the responsability of storing transactions.
authorGustavo Martin Morcuende <gustavo.martin@scmspain.com>
Sun, 4 Dec 2016 16:16:15 +0000 (17:16 +0100)
committerGustavo Martin Morcuende <gustavo.martin@scmspain.com>
Sun, 4 Dec 2016 16:16:15 +0000 (17:16 +0100)
TransactionRepository will store transactions.

TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Account.java
TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/AccountShould.java
TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/PrintStatementFeature.java
TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepository.java

index fb43a95..a24744b 100644 (file)
@@ -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() {
index 9b2c441..07d0c32 100644 (file)
@@ -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);
+       }
 }
index c2c164a..ee1bfbb 100644 (file)
@@ -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
index a8a86f1..08e3ef7 100644 (file)
@@ -6,4 +6,8 @@ public class TransactionRepository {
                throw new UnsupportedOperationException();
        }
 
+       public void addWithdrawal(int amount) {
+               throw new UnsupportedOperationException();
+       }
+
 }