forked from aleixmorgadas/mocks-fakes-spies-and-stubs-kata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRateServiceTest_Mock_Stub_Spy.java
141 lines (105 loc) · 4.3 KB
/
RateServiceTest_Mock_Stub_Spy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package kata.domain.rate;
import kata.domain.film.Film;
import static kata.domain.film.FilmDummy.randomFilm;
import kata.domain.film.FilmService;
import static kata.domain.rate.RateDummy.*;
import kata.domain.user.UserId;
import kata.domain.user.UserIdDummy;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.List;
import java.util.Optional;
class RateServiceTest_Mock_Stub_Spy {
private RateRepository repository;
private FilmService filmService;
private RateService rateService;
private LikedNotifier likedNotifier;
@BeforeEach
void setup() {
repository = Mockito.mock(RateRepository.class);
filmService = Mockito.mock(FilmService.class);
likedNotifier = Mockito.mock(LikedNotifier.class);
rateService = new RateService(repository, filmService, likedNotifier);
}
@Test
void shouldSaveInTheRepository_usingASpy_withMockito() {
final Rate rate = Rate.of("aTitle", 4, UserIdDummy.randomUserId());
// Exercise
rateService.save(rate);
// Verify expectations
}
@Test
void shouldReceiveFromRepository_usingAStub_withMockito() {
final Rate rate = Rate.of("aTitle", 4, UserIdDummy.randomUserId());
// Setup
// Exercise
final Optional<Rate> ratingFromRepo = rateService.findById(rate.id);
// Verify State
}
@Test
void shouldReturnRatesMadeByAUser_usingAStub_withMockito() {
final UserId userId = UserId.of("aUser");
final Rate rateOneByUser = randomRate().withUserId(userId).build();
final Rate rateTwoByUser = randomRate().withUserId(userId).build();
final List<Rate> allRates = randomListOfRatesOfSize(10);
allRates.add(rateOneByUser);
allRates.add(rateTwoByUser);
// Setup
// Exercise
final List<Rate> ratedByUser = rateService.findByUser(userId);
// Verify State
}
@Test
void shouldReturnTheListOfRatesByAUserForAFilmThatWasProducedAtYearOrMoreRecent_usingAStub_withMockito() {
final UserId userId = UserId.of("aUser");
final int productionYear = 2000;
final String theLionKingTitle = "The Lion King";
final Film theLionKingMovieAsOldFilm = randomFilm()
.withTitle(theLionKingTitle)
.withReleaseDate(1994)
.build();
final String frozenTitle = "Frozen";
final Film frozenMovieAsNewerFilm = randomFilm()
.withTitle(frozenTitle)
.withReleaseDate(2013)
.build();
final Rate rateOfFrozenByUser = randomRate()
.withTitle(frozenTitle)
.withUserId(userId)
.build();
final Rate rateOfTheLionKingByUser = randomRate()
.withTitle(theLionKingTitle)
.withUserId(userId)
.build();
final List<Rate> allRates = randomListOfRatesOfSize(10);
allRates.add(rateOfFrozenByUser);
allRates.add(rateOfTheLionKingByUser);
// Setup
// Exercise
final List<Rate> ratesByUserOfFilmsMadeAtYear2000OrMoreRecent = rateService
.ratedByUserAtYearOrMoreRecent(userId, productionYear);
// Verify State
}
@Test
void whenAFilmIsRatedMoreThan10Times_ItShouldNotifyOnceThatItHasBeenLikedBy10DifferentUsers_usingASpy_withMockito() {
final String title = "aTitle";
final List<Rate> ratesForFilm = randomListOfRatesOfSizeForFilm(RateService.RATES_PER_FILM_FOR_NOTIFICATION, title);
// Setup
// Exercise
rateService.save(randomRate().withTitle(title).build());
// Verify it has been called
}
@Test
void whenAFilmIsRatedMoreThan10Times_ItShouldNotifyOnceThatItHasBeenLikedBy10DifferentUsers_usingAMock_withoutMockito() {
// IMPORTANT: This is a Mockito-free zone!
// You are expected to write your own mock for this test case
final String title = "aTitle";
final Rate rate = randomRate().withTitle(title).build();
final List<Rate> ratesForFilm = randomListOfRatesOfSizeForFilm(RateService.RATES_PER_FILM_FOR_NOTIFICATION, title);
// Setup Expectations
// Exercise
rateService.save(rate);
// Verify expectations
}
}