From: Gustavo Martin Morcuende Date: Sun, 20 Nov 2016 22:21:45 +0000 (+0100) Subject: Creating test for refactoring legacy code. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=2c64bf98bd0b112e95abbd9be41e001ae9220154;p=JavaForFun Creating test for refactoring legacy code. 1. We may not change production code if not covered by tests. 2. Start testing the legacy code following the shortest branch. 3. We should not invoke more classes than the one being under test. 4. We try to create a fluent interface. If we are testing user is not logged in, our test must set the value making the not logged in user (we will call it GUEST) 5. Making clear that the input parameter in getTripsByUser is useless for the Unit Test throw_an_exception_when_user_is_not_logged_in --- 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 7836c95..8fe41f1 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,9 @@ public class TripService { public List getTripsByUser(User user) throws UserNotLoggedInException { List tripList = new ArrayList(); - User loggedUser = UserSession.getInstance().getLoggedUser(); + // In Unit Test we shouldn't invoke other classes :( + // User loggedUser = UserSession.getInstance().getLoggedUser(); + User loggedUser = getLoggedInUser(); boolean isFriend = false; if (loggedUser != null) { for (User friend : user.getFriends()) { @@ -28,5 +30,10 @@ public class TripService { throw new UserNotLoggedInException(); } } + + 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 new file mode 100644 index 0000000..58ee787 --- /dev/null +++ b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java @@ -0,0 +1,30 @@ +package org.craftedsw.tripservicekata.trip; + +import org.craftedsw.tripservicekata.exception.UserNotLoggedInException; +import org.craftedsw.tripservicekata.user.User; +import org.junit.Test; + +public class TripServiceShould { + + private static final User GUEST = null; + private static final User UNUSED_USER = null; + private User loggedInUser; + + @Test(expected=UserNotLoggedInException.class) public void + throw_an_exception_when_user_is_not_logged_in() { + TripService tripService = new TesteableTripService(); + + loggedInUser = GUEST; + + tripService.getTripsByUser(UNUSED_USER); + } + + private class TesteableTripService extends TripService { + + @Override + protected User getLoggedInUser() { + return loggedInUser; + } + + } +} diff --git a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceTest.java b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceTest.java deleted file mode 100644 index 2559aad..0000000 --- a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceTest.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.craftedsw.tripservicekata.trip; - -import org.junit.Test; - -public class TripServiceTest { - - @Test public void - should_throw_an_exception_when_no_logged_user() { - - } -}