TripServiceShould green
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 27 Nov 2016 14:07:07 +0000 (15:07 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 27 Nov 2016 14:07:07 +0000 (15:07 +0100)
TripService is green again, we are using BDDMockito (which integrates nicely with //given //when //then comments)
and we can delete code from our TripServiceShould unit test

TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java
TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java

index cffc596..bd899a2 100644 (file)
@@ -31,7 +31,7 @@ public class TripService {
                return new ArrayList<>();\r
        }\r
 \r
-       protected List<Trip> tripsBy(User user) {\r
+       private List<Trip> tripsBy(User user) {\r
                return tripDAO.tripsBy(user);\r
        }\r
 }\r
index e9ac3ea..1111b54 100644 (file)
@@ -2,6 +2,7 @@ package org.craftedsw.tripservicekata.trip;
 \r
 import static org.hamcrest.core.Is.is;\r
 import static org.junit.Assert.assertThat;\r
+import static org.mockito.BDDMockito.given;\r
 \r
 import java.util.List;\r
 \r
@@ -27,18 +28,16 @@ public class TripServiceShould {
        private static final Trip TO_BERLIN = new Trip();\r
        \r
        @Mock TripDAO tripDAO;\r
-       @InjectMocks @Spy private TripService realTripService;\r
-       private TripService tripService;\r
+       @InjectMocks @Spy private TripService tripService;\r
        \r
        @Before\r
        public void setUp() {\r
-               realTripService = new TripService(tripDAO);\r
-               tripService = new TesteableTripService();\r
+               tripService = new TripService(tripDAO);\r
        }\r
 \r
        @Test(expected=UserNotLoggedInException.class) public void\r
        throw_an_exception_when_user_is_not_logged_in() {               \r
-               realTripService.getTripsByUser(UNUSED_USER, GUEST);\r
+               tripService.getTripsByUser(UNUSED_USER, GUEST);\r
        }\r
        \r
        @Test public void\r
@@ -48,7 +47,7 @@ public class TripServiceShould {
                                                .withTrips(TO_BRAZIL)\r
                                                .build();\r
                \r
-               List<Trip> friendTrips = realTripService.getTripsByUser(friend, REGISTERED_USER); \r
+               List<Trip> friendTrips = tripService.getTripsByUser(friend, REGISTERED_USER); \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
@@ -61,24 +60,12 @@ public class TripServiceShould {
                                                .friendsWith(ANOTHER_USER, REGISTERED_USER)\r
                                                .withTrips(TO_BRAZIL, TO_BERLIN)\r
                                                .build();\r
+               given(tripDAO.tripsBy(friend)).willReturn(friend.trips());\r
                \r
-               List<Trip> friendTrips = realTripService.getTripsByUser(friend, REGISTERED_USER); \r
+               List<Trip> friendTrips = tripService.getTripsByUser(friend, REGISTERED_USER); \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
-               public TesteableTripService() {\r
-                       super(new TripDAO());\r
-               }\r
-\r
-               @Override\r
-               protected List<Trip> tripsBy(User user) {\r
-                       return user.trips();\r
-               }\r
-               \r
-       }\r
 }\r