We must always turn static methods into protected ones.
authorGustavo Martin Morcuende <gustavo.martin@scmspain.com>
Wed, 23 Nov 2016 20:52:26 +0000 (21:52 +0100)
committerGustavo Martin Morcuende <gustavo.martin@scmspain.com>
Wed, 23 Nov 2016 20:52:26 +0000 (21:52 +0100)
It is the only way of testing them (without using PowerMockito and stuff like that)
Remember, every change in the legacy code must be done by the tools proviede by our IDE (Eclipse, InteliJ, etc)
you must never rewrite by hand the legacy code.

TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java
TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java

index 8fe41f1..3eec3a8 100644 (file)
@@ -11,7 +11,8 @@ public class TripService {
 \r
        public List<Trip> getTripsByUser(User user) throws UserNotLoggedInException {\r
                List<Trip> tripList = new ArrayList<Trip>();\r
-               // In Unit Test we shouldn't invoke other classes :(\r
+               // In Unit Test we shouldn't invoke other classes because\r
+               // other classes could be using data base, network, etc, etc.\r
                // User loggedUser = UserSession.getInstance().getLoggedUser();\r
                User loggedUser = getLoggedInUser();\r
                boolean isFriend = false;\r
@@ -23,7 +24,7 @@ public class TripService {
                                }\r
                        }\r
                        if (isFriend) {\r
-                               tripList = TripDAO.findTripsByUser(user);\r
+                               tripList = tripsBy(user);\r
                        }\r
                        return tripList;\r
                } else {\r
@@ -31,6 +32,12 @@ public class TripService {
                }\r
        }\r
 \r
+       protected List<Trip> tripsBy(User user) {\r
+               List<Trip> tripList;\r
+               tripList = TripDAO.findTripsByUser(user);\r
+               return tripList;\r
+       }\r
+\r
        protected User getLoggedInUser() {\r
                User loggedUser = UserSession.getInstance().getLoggedUser();\r
                return loggedUser;\r
index 7243417..b8177d0 100644 (file)
@@ -17,6 +17,7 @@ public class TripServiceShould {
        private static final User REGISTERED_USER = new User();\r
        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
        private User loggedInUser;\r
        private TripService tripService;\r
        \r
@@ -47,12 +48,34 @@ public class TripServiceShould {
                assertThat(friendTrips.size(), is(0));\r
        }\r
        \r
+       @Test public void\r
+       return_friend_trips_when_users_are_friends() {\r
+               loggedInUser = REGISTERED_USER;\r
+               \r
+               User friend = new User();\r
+               friend.addFriend(loggedInUser);\r
+               friend.addFriend(ANOTHER_USER);\r
+               friend.addTrip(TO_BRAZIL);\r
+               friend.addTrip(TO_BERLIN);\r
+               \r
+               List<Trip> friendTrips = tripService.getTripsByUser(friend); \r
+               // You must always begin writing the assert.\r
+               // Remember: the assert must match the unit test method's name!!\r
+               // In this case, no trips must be returned.\r
+               assertThat(friendTrips.size(), is(2));\r
+       }\r
+       \r
        private class TesteableTripService extends TripService {\r
 \r
                @Override\r
                protected User getLoggedInUser() {\r
                        return loggedInUser;\r
                }\r
+\r
+               @Override\r
+               protected List<Trip> tripsBy(User user) {\r
+                       return user.trips();\r
+               }\r
                \r
        }\r
 }\r