From b871c38ab6a408e7ce6f9e8b242a118bca7f69cf Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 23 Nov 2016 21:52:26 +0100 Subject: [PATCH] We must always turn static methods into protected ones. 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. --- .../tripservicekata/trip/TripService.java | 11 +++++++++-- .../tripservicekata/trip/TripServiceShould.java | 23 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java index 8fe41f1..3eec3a8 100644 --- a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java @@ -11,7 +11,8 @@ public class TripService { public List getTripsByUser(User user) throws UserNotLoggedInException { List tripList = new ArrayList(); - // In Unit Test we shouldn't invoke other classes :( + // In Unit Test we shouldn't invoke other classes because + // other classes could be using data base, network, etc, etc. // User loggedUser = UserSession.getInstance().getLoggedUser(); User loggedUser = getLoggedInUser(); boolean isFriend = false; @@ -23,7 +24,7 @@ public class TripService { } } if (isFriend) { - tripList = TripDAO.findTripsByUser(user); + tripList = tripsBy(user); } return tripList; } else { @@ -31,6 +32,12 @@ public class TripService { } } + protected List tripsBy(User user) { + List tripList; + tripList = TripDAO.findTripsByUser(user); + return tripList; + } + protected User getLoggedInUser() { User loggedUser = UserSession.getInstance().getLoggedUser(); return loggedUser; 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 7243417..b8177d0 100644 --- a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java +++ b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java @@ -17,6 +17,7 @@ public class TripServiceShould { private static final User REGISTERED_USER = new User(); private static final User ANOTHER_USER = new User(); private static final Trip TO_BRAZIL = new Trip(); + private static final Trip TO_BERLIN = new Trip(); private User loggedInUser; private TripService tripService; @@ -47,12 +48,34 @@ public class TripServiceShould { assertThat(friendTrips.size(), is(0)); } + @Test public void + return_friend_trips_when_users_are_friends() { + loggedInUser = REGISTERED_USER; + + User friend = new User(); + friend.addFriend(loggedInUser); + friend.addFriend(ANOTHER_USER); + friend.addTrip(TO_BRAZIL); + friend.addTrip(TO_BERLIN); + + List friendTrips = tripService.getTripsByUser(friend); + // You must always begin writing the assert. + // Remember: the assert must match the unit test method's name!! + // In this case, no trips must be returned. + assertThat(friendTrips.size(), is(2)); + } + private class TesteableTripService extends TripService { @Override protected User getLoggedInUser() { return loggedInUser; } + + @Override + protected List tripsBy(User user) { + return user.trips(); + } } } -- 2.1.4