From: Gustavo Martin Morcuende Date: Sun, 4 Dec 2016 17:13:27 +0000 (+0100) Subject: Tests are failing for the right reasons X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=447e4733f0e6051071fb52ea60a5e1d5dd255553;p=JavaForFun Tests are failing for the right reasons They throw UnsupportedOperationException because we need the implementations. --- 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); + } + +}