Creating test for refactoring legacy code.
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 20 Nov 2016 22:21:45 +0000 (23:21 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 20 Nov 2016 22:21:45 +0000 (23:21 +0100)
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

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

index 7836c95..8fe41f1 100644 (file)
@@ -11,7 +11,9 @@ public class TripService {
 \r
        public List<Trip> getTripsByUser(User user) throws UserNotLoggedInException {\r
                List<Trip> tripList = new ArrayList<Trip>();\r
-               User loggedUser = UserSession.getInstance().getLoggedUser();\r
+               // In Unit Test we shouldn't invoke other classes :(\r
+               // User loggedUser = UserSession.getInstance().getLoggedUser();\r
+               User loggedUser = getLoggedInUser();\r
                boolean isFriend = false;\r
                if (loggedUser != null) {\r
                        for (User friend : user.getFriends()) {\r
@@ -28,5 +30,10 @@ public class TripService {
                        throw new UserNotLoggedInException();\r
                }\r
        }\r
+\r
+       protected User getLoggedInUser() {\r
+               User loggedUser = UserSession.getInstance().getLoggedUser();\r
+               return loggedUser;\r
+       }\r
        \r
 }\r
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 (file)
index 0000000..58ee787
--- /dev/null
@@ -0,0 +1,30 @@
+package org.craftedsw.tripservicekata.trip;\r
+\r
+import org.craftedsw.tripservicekata.exception.UserNotLoggedInException;\r
+import org.craftedsw.tripservicekata.user.User;\r
+import org.junit.Test;\r
+\r
+public class TripServiceShould {\r
+       \r
+       private static final User GUEST = null;\r
+       private static final User UNUSED_USER = null;\r
+       private User loggedInUser;\r
+\r
+       @Test(expected=UserNotLoggedInException.class) public void\r
+       throw_an_exception_when_user_is_not_logged_in() {\r
+               TripService tripService = new TesteableTripService();\r
+               \r
+               loggedInUser = GUEST;\r
+               \r
+               tripService.getTripsByUser(UNUSED_USER);\r
+       }\r
+       \r
+       private class TesteableTripService extends TripService {\r
+\r
+               @Override\r
+               protected User getLoggedInUser() {\r
+                       return loggedInUser;\r
+               }\r
+               \r
+       }\r
+}\r
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 (file)
index 2559aad..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-package org.craftedsw.tripservicekata.trip;\r
-\r
-import org.junit.Test;\r
-\r
-public class TripServiceTest {\r
-       \r
-       @Test public void\r
-       should_throw_an_exception_when_no_logged_user() {\r
-               \r
-       }\r
-}\r