Fixing coverage in Clock.today()
authorGustavo Martin Morcuende <gustavo.martin@scmspain.com>
Sun, 4 Dec 2016 22:09:17 +0000 (23:09 +0100)
committerGustavo Martin Morcuende <gustavo.martin@scmspain.com>
Sun, 4 Dec 2016 22:09:17 +0000 (23:09 +0100)
My implementation solves the coverage issue (hopefully it is right)

TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/Clock.java
TDD/sandromancuso/bank/src/main/java/org/craftedsw/feature/ClockGus.java [new file with mode: 0644]
TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/ClockGusShould.java [new file with mode: 0644]

index 30c7150..059d817 100644 (file)
@@ -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 (file)
index 0000000..cdad1ba
--- /dev/null
@@ -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 (file)
index 0000000..866466e
--- /dev/null
@@ -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();
+       }
+}