\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
throw new UserNotLoggedInException();\r
}\r
}\r
+\r
+ protected User getLoggedInUser() {\r
+ User loggedUser = UserSession.getInstance().getLoggedUser();\r
+ return loggedUser;\r
+ }\r
\r
}\r
--- /dev/null
+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
+++ /dev/null
-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