Tests are failing for the right reasons
authorGustavo Martin Morcuende <gustavo.martin@scmspain.com>
Sun, 4 Dec 2016 17:13:27 +0000 (18:13 +0100)
committerGustavo Martin Morcuende <gustavo.martin@scmspain.com>
Sun, 4 Dec 2016 17:13:27 +0000 (18:13 +0100)
They throw UnsupportedOperationException because we need the implementations.

TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Clock.java [new file with mode: 0644]
TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Transaction.java
TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/TransactionRepository.java [new file with mode: 0644]
TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/AccountShould.java
TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepository.java [deleted file]
TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepositoryShould.java [new file with mode: 0644]

diff --git a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Clock.java b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Clock.java
new file mode 100644 (file)
index 0000000..41d35bf
--- /dev/null
@@ -0,0 +1,9 @@
+package org.craftedsw.feature;
+
+public class Clock {
+
+       public String todayAsString() {
+               throw new UnsupportedOperationException();
+       }
+
+}
index 69e932f..2786699 100644 (file)
@@ -2,4 +2,8 @@ package org.craftedsw.feature;
 
 public class Transaction {
 
+       public Transaction(String date, int amount) {
+               throw new UnsupportedOperationException();
+       }
+
 }
diff --git a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/TransactionRepository.java b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/TransactionRepository.java
new file mode 100644 (file)
index 0000000..499e87e
--- /dev/null
@@ -0,0 +1,19 @@
+package org.craftedsw.feature;
+
+import java.util.List;
+
+public class TransactionRepository {
+
+       public void addDeposit(int amount) {
+               throw new UnsupportedOperationException();
+       }
+
+       public void addWithdrawal(int amount) {
+               throw new UnsupportedOperationException();
+       }
+
+       public List<Transaction> allTransactions() {
+               throw new UnsupportedOperationException();
+       }
+
+}
index 4a5b781..5daf49c 100644 (file)
@@ -40,7 +40,7 @@ public class AccountShould {
        
        @Test public void
        print_a_statement() {
-               List<Transaction> transactions = asList(new Transaction());
+               List<Transaction> transactions = asList(new Transaction("04/12/2016", 100));
                given(transactionRepository.allTransactions()).willReturn(transactions);
                
                account.printStatement();
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
deleted file mode 100644 (file)
index 499e87e..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-package org.craftedsw.feature;
-
-import java.util.List;
-
-public class TransactionRepository {
-
-       public void addDeposit(int amount) {
-               throw new UnsupportedOperationException();
-       }
-
-       public void addWithdrawal(int amount) {
-               throw new UnsupportedOperationException();
-       }
-
-       public List<Transaction> allTransactions() {
-               throw new UnsupportedOperationException();
-       }
-
-}
diff --git a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepositoryShould.java b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepositoryShould.java
new file mode 100644 (file)
index 0000000..c661acd
--- /dev/null
@@ -0,0 +1,47 @@
+package org.craftedsw.feature;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.BDDMockito.given;
+
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+// This would be an integration test because underneath there should be
+// a database. For the sake of simplicity we will have an in memory
+// repository.
+@RunWith(MockitoJUnitRunner.class)
+public class TransactionRepositoryShould {
+
+       private static final String TODAY = "04/12/2016";
+
+       @Mock private Clock clock;
+       
+       private TransactionRepository transactionRepository;
+       
+       @Before
+       public void initialise() {
+               transactionRepository = new TransactionRepository();
+       }
+
+       @Test public void
+       create_and_store_a_deposit_transaction() {
+               given(clock.todayAsString()).willReturn(TODAY);
+               transactionRepository.addDeposit(100);
+               
+               List<Transaction> transactions = transactionRepository.allTransactions();
+               
+               assertThat(transactions.size(), is(1));
+               assertThat(transactions.get(0), is(transaction(TODAY, 100)));
+       }
+
+       private Transaction transaction(String date, int amount) {
+               return new Transaction(date, amount);
+       }
+
+}