Improvement with fluent interface for our test
authorGustavo Martin Morcuende <gustavo.martin@scmspain.com>
Thu, 24 Nov 2016 00:50:37 +0000 (01:50 +0100)
committerGustavo Martin Morcuende <gustavo.martin@scmspain.com>
Thu, 24 Nov 2016 00:50:37 +0000 (01:50 +0100)
TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java

index 3b87700..e2a58ba 100644 (file)
@@ -36,9 +36,10 @@ public class TripServiceShould {
        \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
@@ -48,12 +49,11 @@ public class TripServiceShould {
        }\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
@@ -62,6 +62,45 @@ public class TripServiceShould {
                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