\r
@Test public void\r
not_return_any_trips_when_users_are_not_friends() { \r
- User friend = new User();\r
- friend.addFriend(ANOTHER_USER);\r
- friend.addTrip(TO_BRAZIL);\r
+ User friend = UserBuilder.aUser()\r
+ .friendsWith(ANOTHER_USER)\r
+ .withTrips(TO_BRAZIL)\r
+ .build();\r
\r
List<Trip> friendTrips = tripService.getTripsByUser(friend); \r
// You must always begin writing the assert.\r
}\r
\r
@Test public void\r
- return_friend_trips_when_users_are_friends() { \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
+ return_friend_trips_when_users_are_friends() {\r
+ User friend = UserBuilder.aUser()\r
+ .friendsWith(ANOTHER_USER, loggedInUser)\r
+ .withTrips(TO_BRAZIL, TO_BERLIN)\r
+ .build();\r
\r
List<Trip> friendTrips = tripService.getTripsByUser(friend); \r
// You must always begin writing the assert.\r
assertThat(friendTrips.size(), is(2));\r
}\r
\r
+ \r
+ public static class UserBuilder {\r
+ private User[] friends = new User[]{};\r
+ private Trip[] trips = new Trip[]{};\r
+ \r
+ public static UserBuilder aUser() {\r
+ return new UserBuilder();\r
+ }\r
+\r
+ public UserBuilder withTrips(Trip...trips) {\r
+ this.trips = trips;\r
+ return this;\r
+ }\r
+\r
+ public UserBuilder friendsWith(User...friends) {\r
+ this.friends = friends;\r
+ return this;\r
+ }\r
+ \r
+ public User build() {\r
+ User user = new User();\r
+ addTripsTo(user);\r
+ addFriendsTo(user);\r
+ return user;\r
+ }\r
+\r
+ private void addFriendsTo(User user) {\r
+ for (User friend : friends) {\r
+ user.addFriend(friend);\r
+ } \r
+ }\r
+\r
+ private void addTripsTo(User user) {\r
+ for (Trip trip : trips) {\r
+ user.addTrip(trip);\r
+ }\r
+ }\r
+ }\r
+ \r
private class TesteableTripService extends TripService {\r
\r
@Override\r