TripDAO, always test before refactoring
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 27 Nov 2016 13:26:05 +0000 (14:26 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 27 Nov 2016 13:26:05 +0000 (14:26 +0100)
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)

TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripDAO.java
TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOShould.java [new file with mode: 0644]
TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripDAOTest.java [deleted file]
TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java

index f054f60..ab859ed 100644 (file)
@@ -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<Trip> 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 (file)
index 0000000..34f6cc5
--- /dev/null
@@ -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 (file)
index 6482bca..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-package org.craftedsw.tripservicekata.trip;
-
-public class TripDAOTest {
-
-}
index 9836194..1151d86 100644 (file)
@@ -10,7 +10,11 @@ import org.craftedsw.tripservicekata.exception.UserNotLoggedInException;
 import org.craftedsw.tripservicekata.user.User;\r
 import org.junit.Before;\r
 import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.Mock;\r
+import org.mockito.runners.MockitoJUnitRunner;\r
 \r
+@RunWith(MockitoJUnitRunner.class)\r
 public class TripServiceShould {\r
        \r
        private static final User GUEST = null;\r
@@ -19,6 +23,9 @@ public class TripServiceShould {
        private static final User ANOTHER_USER = new User();\r
        private static final Trip TO_BRAZIL = new Trip();\r
        private static final Trip TO_BERLIN = new Trip();\r
+       \r
+       @Mock TripDAO tripDAO;\r
+       private TripService realTripService;\r
        private TripService tripService;\r
        \r
        @Before\r