From 1a16367affa0b8571b6ca1b8beee2aaf3e6f81dd Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 4 Dec 2016 23:09:17 +0100 Subject: [PATCH] Fixing coverage in Clock.today() My implementation solves the coverage issue (hopefully it is right) --- .../src/main/java/org/craftedsw/feature/Clock.java | 1 + .../main/java/org/craftedsw/feature/ClockGus.java | 17 +++++++++ .../java/org/craftedsw/feature/ClockGusShould.java | 43 ++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/ClockGus.java create mode 100644 TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/ClockGusShould.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 index 30c7150..059d817 100644 --- a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Clock.java +++ b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Clock.java @@ -16,6 +16,7 @@ public class Clock { // QUESTION FOR SANDRO MANCUSO: // This line of code is not under test, so if someone makes // a change here our unit tests will not fail!!! :/ + // See possible answer in my ClockGus and ClockGusShould implementations. return LocalDate.now(); // If someone writes: // return null diff --git a/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/ClockGus.java b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/ClockGus.java new file mode 100644 index 0000000..cdad1ba --- /dev/null +++ b/TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/ClockGus.java @@ -0,0 +1,17 @@ +package org.craftedsw.feature; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class ClockGus { + + private static final DateTimeFormatter DD_MM_YYYY = DateTimeFormatter.ofPattern("dd/MM/yyyy"); + + public String todayAsString() { + return formattedToday(LocalDate.now()); + } + + protected String formattedToday(LocalDate date) { + return date.format(DD_MM_YYYY); + } +} diff --git a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/ClockGusShould.java b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/ClockGusShould.java new file mode 100644 index 0000000..866466e --- /dev/null +++ b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/ClockGusShould.java @@ -0,0 +1,43 @@ +package org.craftedsw.feature; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +import org.junit.Test; + +public class ClockGusShould { + private static final DateTimeFormatter DD_MM_YYYY = DateTimeFormatter.ofPattern("dd/MM/yyyy"); + + @Test public void + return_todays_date_in_dd_MM_yyyy_format() { + ClockGus clock = spy(new ClockGus()); + LocalDate today = today(); + given(clock.formattedToday(today)).willReturn(today.format(DD_MM_YYYY)); + + String date = clock.todayAsString(); + + assertThat(date, is(today.format(DD_MM_YYYY))); + verify(clock, times(1)).formattedToday(today); + } + + @Test public void + return_date_in_dd_MM_yyyy_format() { + ClockGus clock = new ClockGus(); + LocalDate today = today(); + + String date = clock.formattedToday(today); + + assertThat(date, is(today.format(DD_MM_YYYY))); + } + + private LocalDate today() { + return LocalDate.now(); + } +} -- 2.1.4