\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
}\r
}\r
if (isFriend) {\r
- tripList = TripDAO.findTripsByUser(user);\r
+ tripList = tripsBy(user);\r
}\r
return tripList;\r
} else {\r
}\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
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
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