From: Gustavo Martin Morcuende Date: Tue, 6 Dec 2016 15:12:20 +0000 (+0100) Subject: Fixing coverage in Clock.today() X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=ccabe212432a12d704d1512224fc8cc0f908f110;p=JavaForFun Fixing coverage in Clock.today() Ivan Lorenz solution. --- 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 059d817..e8e9215 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 @@ -19,7 +19,7 @@ public class Clock { // See possible answer in my ClockGus and ClockGusShould implementations. return LocalDate.now(); // If someone writes: - // return null + // return null; // nothing happens, tests keep going green... :( } diff --git a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/ClockShould.java b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/ClockShould.java index fd54ef7..e3a8af5 100644 --- a/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/ClockShould.java +++ b/TDD/sandromancuso/bank/src/test/java/org/craftedsw/feature/ClockShould.java @@ -4,27 +4,55 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import org.junit.Test; public class ClockShould { - @Test public void - return_todays_date_in_dd_MM_yyyy_format() { - Clock clock = new TestableClock(); - - String date = clock.todayAsString(); - - assertThat(date, is("04/12/2016")); - } - - private class TestableClock extends Clock { - - @Override - protected LocalDate today() { - // We will control the random part of our code - // (no need of using spy) - return LocalDate.of(2016, 12, 4); - } - } +// Sandro Mancuso original solution: +// @Test public void +// return_todays_date_in_dd_MM_yyyy_format() { +// Clock clock = new TestableClock(); +// +// String date = clock.todayAsString(); +// +// assertThat(date, is("04/12/2016")); +// } +// +// private class TestableClock extends Clock { +// +// @Override +// protected LocalDate today() { +// // We will control the random part of our code +// // (no need of using spy) +// return LocalDate.of(2016, 12, 4); +// } +// } + +// Ivan Lorenz solution for the coverage problem: + private static final DateTimeFormatter DD_MM_YYYY = DateTimeFormatter.ofPattern("dd/MM/yyyy"); + + // We will control the random part of our code + private LocalDate today; + + + @Test public void + return_todays_date_in_dd_MM_yyyy_format () { + Clock clock = new TestableClock(); + + String date = clock.todayAsString(); + + assertThat(date, is(today.format(DD_MM_YYYY))); + } + + private class TestableClock extends Clock { + + @Override + protected LocalDate today() { + // We will control the random part of our code + today = super.today(); + return today; + } + } }