From d6e483e15609a45fc0de16ef6d56a196ce0f3cd5 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 27 Nov 2016 15:07:07 +0100 Subject: [PATCH] TripServiceShould green 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 --- .../tripservicekata/trip/TripService.java | 2 +- .../tripservicekata/trip/TripServiceShould.java | 27 ++++++---------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java index cffc596..bd899a2 100644 --- a/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java +++ b/TDD/src/main/java/org/craftedsw/tripservicekata/trip/TripService.java @@ -31,7 +31,7 @@ public class TripService { return new ArrayList<>(); } - protected List tripsBy(User user) { + private List tripsBy(User user) { return tripDAO.tripsBy(user); } } diff --git a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java index e9ac3ea..1111b54 100644 --- a/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java +++ b/TDD/src/test/java/org/craftedsw/tripservicekata/trip/TripServiceShould.java @@ -2,6 +2,7 @@ package org.craftedsw.tripservicekata.trip; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; +import static org.mockito.BDDMockito.given; import java.util.List; @@ -27,18 +28,16 @@ public class TripServiceShould { private static final Trip TO_BERLIN = new Trip(); @Mock TripDAO tripDAO; - @InjectMocks @Spy private TripService realTripService; - private TripService tripService; + @InjectMocks @Spy private TripService tripService; @Before public void setUp() { - realTripService = new TripService(tripDAO); - tripService = new TesteableTripService(); + tripService = new TripService(tripDAO); } @Test(expected=UserNotLoggedInException.class) public void throw_an_exception_when_user_is_not_logged_in() { - realTripService.getTripsByUser(UNUSED_USER, GUEST); + tripService.getTripsByUser(UNUSED_USER, GUEST); } @Test public void @@ -48,7 +47,7 @@ public class TripServiceShould { .withTrips(TO_BRAZIL) .build(); - List friendTrips = realTripService.getTripsByUser(friend, REGISTERED_USER); + List friendTrips = tripService.getTripsByUser(friend, REGISTERED_USER); // You must always begin writing the assert. // Remember: the assert must match the unit test method's name!! // In this case, no trips must be returned. @@ -61,24 +60,12 @@ public class TripServiceShould { .friendsWith(ANOTHER_USER, REGISTERED_USER) .withTrips(TO_BRAZIL, TO_BERLIN) .build(); + given(tripDAO.tripsBy(friend)).willReturn(friend.trips()); - List friendTrips = realTripService.getTripsByUser(friend, REGISTERED_USER); + List friendTrips = tripService.getTripsByUser(friend, REGISTERED_USER); // You must always begin writing the assert. // Remember: the assert must match the unit test method's name!! // In this case, no trips must be returned. assertThat(friendTrips.size(), is(2)); } - - private class TesteableTripService extends TripService { - - public TesteableTripService() { - super(new TripDAO()); - } - - @Override - protected List tripsBy(User user) { - return user.trips(); - } - - } } -- 2.1.4