From: Gustavo Martin Morcuende Date: Sun, 27 Nov 2016 13:26:05 +0000 (+0100) Subject: TripDAO, always test before refactoring X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=d810fb5b1bbf8e28eddbf34b2b5cfcf14e94e049;p=JavaForFun TripDAO, always test before refactoring We need a new method in order to be able to rewrite our legacy code. At the very end this method will be the one used by our app. The static method will dissapear after refactoring our whole app (what should be done carefully) --- diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java index f054f60..ab859ed 100644 --- a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java @@ -11,5 +11,17 @@ public class TripDAO { throw new CollaboratorCallException( "TripDAO should not be invoked on an unit test."); } + + // At the end and by means of many code refactoring our app + // will end up using the instance method. In the meanwhile we + // will not be able to remove the static method but at least + // we are offering something that can be tested. + // Be careful when refactoring this code because in real life + // findTripsByUser for sure will be used in many places but again + // in the meanwhile with this code we can write unit tests (we + // always need to write a test of our legacy code before refactoring it) + public List tripsBy(User user) { + return TripDAO.findTripsByUser(user); + } } \ No newline at end of file diff --git a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java new file mode 100644 index 0000000..34f6cc5 --- /dev/null +++ b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java @@ -0,0 +1,13 @@ +package org.craftedsw.tripservicekata.trip; + +import org.craftedsw.tripservicekata.exception.CollaboratorCallException; +import org.craftedsw.tripservicekata.user.User; +import org.junit.Test; + +public class TripDAOShould { + + @Test(expected = CollaboratorCallException.class) public void + throw_exception_when_retrieving_user_trips() { + new TripDAO().tripsBy(new User()); + } +} diff --git a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOTest.java b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOTest.java deleted file mode 100644 index 6482bca..0000000 --- a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOTest.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.craftedsw.tripservicekata.trip; - -public class TripDAOTest { - -} diff --git a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java index 9836194..1151d86 100644 --- a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java +++ b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java @@ -10,7 +10,11 @@ import org.craftedsw.tripservicekata.exception.UserNotLoggedInException; import org.craftedsw.tripservicekata.user.User; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +@RunWith(MockitoJUnitRunner.class) public class TripServiceShould { private static final User GUEST = null; @@ -19,6 +23,9 @@ public class TripServiceShould { private static final User ANOTHER_USER = new User(); private static final Trip TO_BRAZIL = new Trip(); private static final Trip TO_BERLIN = new Trip(); + + @Mock TripDAO tripDAO; + private TripService realTripService; private TripService tripService; @Before