From 447e4733f0e6051071fb52ea60a5e1d5dd255553 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 4 Dec 2016 18:13:27 +0100 Subject: [PATCH] Tests are failing for the right reasons They throw UnsupportedOperationException because we need the implementations. --- .../src/main/java/org/craftedsw/feature/Clock.java | 9 +++++ .../java/org/craftedsw/feature/Transaction.java | 4 ++ .../craftedsw/feature/TransactionRepository.java | 19 +++++++++ .../java/org/craftedsw/feature/AccountShould.java | 2 +- .../craftedsw/feature/TransactionRepository.java | 19 --------- .../feature/TransactionRepositoryShould.java | 47 ++++++++++++++++++++++ 6 files changed, 80 insertions(+), 20 deletions(-) create mode 100644 TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Clock.java create mode 100644 TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/TransactionRepository.java delete mode 100644 TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepository.java create mode 100644 TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepositoryShould.java 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 index 0000000..41d35bf --- /dev/null +++ b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Clock.java @@ -0,0 +1,9 @@ +package org.craftedsw.feature; + +public class Clock { + + public String todayAsString() { + throw new UnsupportedOperationException(); + } + +} diff --git a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Transaction.java b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Transaction.java index 69e932f..2786699 100644 --- a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Transaction.java +++ b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Transaction.java @@ -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 index 0000000..499e87e --- /dev/null +++ b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/TransactionRepository.java @@ -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 allTransactions() { + throw new UnsupportedOperationException(); + } + +} 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 4a5b781..5daf49c 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 @@ -40,7 +40,7 @@ public class AccountShould { @Test public void print_a_statement() { - List transactions = asList(new Transaction()); + List 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 index 499e87e..0000000 --- a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepository.java +++ /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 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 index 0000000..c661acd --- /dev/null +++ b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/TransactionRepositoryShould.java @@ -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 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); + } + +} -- 2.1.4