11package com .answerdigital .answerking .service ;
22
3- import com .answerdigital .answerking .builder .AddCategoryRequestTestBuilder ;
3+ import com .answerdigital .answerking .builder .CategoryRequestTestBuilder ;
44import com .answerdigital .answerking .builder .CategoryTestBuilder ;
55import com .answerdigital .answerking .builder .ProductTestBuilder ;
6- import com .answerdigital .answerking .builder .UpdateCategoryRequestTestBuilder ;
76import com .answerdigital .answerking .exception .custom .NameUnavailableException ;
87import com .answerdigital .answerking .exception .custom .ProductAlreadyPresentException ;
98import com .answerdigital .answerking .exception .custom .RetirementException ;
1312import com .answerdigital .answerking .repository .CategoryRepository ;
1413import com .answerdigital .answerking .request .CategoryRequest ;
1514import com .answerdigital .answerking .response .CategoryResponse ;
16- import org .junit .jupiter .api .AfterEach ;
17- import org .junit .jupiter .api .BeforeEach ;
1815import org .junit .jupiter .api .Test ;
1916import org .junit .jupiter .api .extension .ExtendWith ;
17+ import org .mockito .InjectMocks ;
2018import org .mockito .Mock ;
2119import org .mockito .junit .jupiter .MockitoExtension ;
2220
2927import static org .mockito .ArgumentMatchers .anyLong ;
3028import static org .mockito .ArgumentMatchers .anyString ;
3129import static org .mockito .Mockito .any ;
32- import static org .mockito .Mockito .doReturn ;
3330import static org .mockito .Mockito .verify ;
31+ import static org .mockito .Mockito .when ;
3432
3533@ ExtendWith (MockitoExtension .class )
3634final class CategoryServiceTest {
3735
36+ @ InjectMocks
3837 private CategoryService categoryService ;
3938
4039 @ Mock
@@ -45,40 +44,27 @@ final class CategoryServiceTest {
4544
4645 private final CategoryTestBuilder categoryTestBuilder ;
4746
48- private final AddCategoryRequestTestBuilder addCategoryRequestTestBuilder ;
49-
50- private final UpdateCategoryRequestTestBuilder updateCategoryRequestTestBuilder ;
47+ private final CategoryRequestTestBuilder categoryRequestTestBuilder ;
5148
5249 private final ProductTestBuilder productTestBuilder ;
5350
54- private static final Long NONEXISTENT_CATEGORY_ID = 2L ;
51+ private static final Long NONEXISTENT_CATEGORY_ID = 10L ;
5552
5653 private CategoryServiceTest () {
5754 categoryTestBuilder = new CategoryTestBuilder ();
58- addCategoryRequestTestBuilder = new AddCategoryRequestTestBuilder ();
59- updateCategoryRequestTestBuilder = new UpdateCategoryRequestTestBuilder ();
55+ categoryRequestTestBuilder = new CategoryRequestTestBuilder ();
6056 productTestBuilder = new ProductTestBuilder ();
6157 }
6258
63- @ BeforeEach
64- void setUp () {
65- categoryService = new CategoryService (productService , categoryRepository );
66- }
67-
68- @ AfterEach
69- void tearDown () {
70- categoryService = null ;
71- }
72-
7359 @ Test
7460 void testAddCategory () {
7561 // given
76- final CategoryRequest addCategoryRequest = addCategoryRequestTestBuilder . withDefaultValues ().build ();
62+ final CategoryRequest addCategoryRequest = categoryRequestTestBuilder . withDefaultAddRequestValues ().build ();
7763 final Category expectedResponse = categoryTestBuilder .withDefaultValues ().build ();
7864
7965 // when
80- doReturn ( false ). when (categoryRepository ) .existsByName (anyString ());
81- doReturn ( expectedResponse ). when (categoryRepository ) .save (any (Category .class ));
66+ when (categoryRepository .existsByName (anyString ())). thenReturn ( false );
67+ when (categoryRepository .save (any (Category .class ))). thenReturn ( expectedResponse );
8268 final CategoryResponse categoryResponse = categoryService .addCategory (addCategoryRequest );
8369
8470 // then
@@ -94,10 +80,10 @@ void testAddCategory() {
9480 @ Test
9581 void testAddCategoryThatAlreadyExists () {
9682 // given
97- final CategoryRequest addCategoryRequest = addCategoryRequestTestBuilder . withDefaultValues ().build ();
83+ final CategoryRequest addCategoryRequest = categoryRequestTestBuilder . withDefaultAddRequestValues ().build ();
9884
9985 // when
100- doReturn ( true ). when (categoryRepository ) .existsByName (anyString ());
86+ when (categoryRepository .existsByName (anyString ())). thenReturn ( true );
10187 final Exception exception = assertThrows (NameUnavailableException .class ,
10288 () -> categoryService .addCategory (addCategoryRequest ));
10389
@@ -110,27 +96,21 @@ void testAddCategoryThatAlreadyExists() {
11096 void testUpdateCategory () {
11197 // given
11298 final Category existingCategory = categoryTestBuilder .withDefaultValues ().build ();
113- final CategoryRequest updateCategoryRequest = updateCategoryRequestTestBuilder .withDefaultValues ().build ();
114- final Category expectedResponse = categoryTestBuilder
115- .withDefaultValues ()
99+ final CategoryRequest updateCategoryRequest = categoryRequestTestBuilder .withDefaultUpdateRequestValues ().build ();
100+ final Category expectedResponse = categoryTestBuilder .withDefaultValues ()
116101 .withName (updateCategoryRequest .name ())
117102 .withDescription (updateCategoryRequest .description ())
118103 .build ();
119- final var categoryResponse = CategoryResponse .builder ()
120- .id (1L )
121- .name ("Pizzas" )
122- .description ("Italian style stone baked pizzas." )
123- .build ();
124104
125105 // when
126- doReturn ( false ). when (categoryRepository ) .existsByNameAndIdIsNot (anyString (), anyLong ());
127- doReturn ( Optional . of ( existingCategory )). when ( categoryRepository ). findById ( anyLong ( ));
128- doReturn ( expectedResponse ). when (categoryRepository ) .save (any (Category .class ));
106+ when (categoryRepository .existsByNameAndIdIsNot (anyString (), anyLong ())). thenReturn ( false );
107+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . of ( existingCategory ));
108+ when (categoryRepository .save (any (Category .class ))). thenReturn ( expectedResponse );
129109
130110 final CategoryResponse response = categoryService .updateCategory (updateCategoryRequest , existingCategory .getId ());
131111
132112 // then
133- assertEquals (categoryResponse .getName (), response .getName ());
113+ assertEquals (expectedResponse .getName (), response .getName ());
134114 verify (categoryRepository ).existsByNameAndIdIsNot (anyString (), anyLong ());
135115 verify (categoryRepository ).findById (anyLong ());
136116 verify (categoryRepository ).save (any (Category .class ));
@@ -139,11 +119,11 @@ void testUpdateCategory() {
139119 @ Test
140120 void testUpdateCategoryThatDoesNotExist () {
141121 // given
142- final CategoryRequest updateCategoryRequest = updateCategoryRequestTestBuilder . withDefaultValues ().build ();
122+ final CategoryRequest updateCategoryRequest = categoryRequestTestBuilder . withDefaultUpdateRequestValues ().build ();
143123
144124 // when
145- doReturn ( false ). when (categoryRepository ) .existsByNameAndIdIsNot (anyString (), anyLong ());
146- doReturn ( Optional . empty ()). when ( categoryRepository ). findById ( anyLong ());
125+ when (categoryRepository .existsByNameAndIdIsNot (anyString (), anyLong ())). thenReturn ( false );
126+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . empty ());
147127 final Exception exception = assertThrows (NotFoundException .class ,
148128 () -> categoryService .updateCategory (updateCategoryRequest , NONEXISTENT_CATEGORY_ID ));
149129
@@ -157,10 +137,10 @@ void testUpdateCategoryThatDoesNotExist() {
157137 void testUpdateCategoryNameToCategoryThatAlreadyExists () {
158138 // given
159139 final Category existingCategory = categoryTestBuilder .withDefaultValues ().build ();
160- final CategoryRequest updateCategoryRequest = updateCategoryRequestTestBuilder . withDefaultValues ().build ();
140+ final CategoryRequest updateCategoryRequest = categoryRequestTestBuilder . withDefaultUpdateRequestValues ().build ();
161141
162142 // when
163- doReturn ( true ). when (categoryRepository ) .existsByNameAndIdIsNot (anyString (), anyLong ());
143+ when (categoryRepository .existsByNameAndIdIsNot (anyString (), anyLong ())). thenReturn ( true );
164144 final Exception exception = assertThrows (NameUnavailableException .class ,
165145 () -> categoryService .updateCategory (updateCategoryRequest , existingCategory .getId ()));
166146
@@ -173,14 +153,13 @@ void testUpdateCategoryNameToCategoryThatAlreadyExists() {
173153 void testAddProductToCategoryThatIsAlreadyInCategory () {
174154 // given
175155 final Product product = productTestBuilder .withDefaultValues ().build ();
176- final Category category = categoryTestBuilder
177- .withDefaultValues ()
156+ final Category category = categoryTestBuilder .withDefaultValues ()
178157 .withProduct (product )
179158 .build ();
180159
181160 // when
182- doReturn ( Optional . of ( category )). when ( categoryRepository ). findById ( anyLong ( ));
183- doReturn ( product ). when (productService ) .findById (anyLong ());
161+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . of ( category ));
162+ when (productService .findById (anyLong ())). thenReturn ( product );
184163 final Exception exception = assertThrows (ProductAlreadyPresentException .class ,
185164 () -> categoryService .addProductToCategory (category .getId (), product .getId ()));
186165
@@ -196,7 +175,7 @@ void testAddProductToCategoryThatDoesNotExist() {
196175 final Product product = productTestBuilder .withDefaultValues ().build ();
197176
198177 // when
199- doReturn ( Optional . empty ()). when ( categoryRepository ). findById ( anyLong ());
178+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . empty ());
200179 final Exception exception = assertThrows (NotFoundException .class ,
201180 () -> categoryService .addProductToCategory (NONEXISTENT_CATEGORY_ID , product .getId ()));
202181
@@ -209,16 +188,15 @@ void testAddProductToCategoryThatDoesNotExist() {
209188 void testRemoveProductFromCategory () {
210189 // given
211190 final Product product = productTestBuilder .withDefaultValues ().build ();
212- final Category category = categoryTestBuilder
213- .withDefaultValues ()
191+ final Category category = categoryTestBuilder .withDefaultValues ()
214192 .withProduct (product )
215193 .build ();
216194 final Category expectedResponse = categoryTestBuilder .withDefaultValues ().build ();
217195
218196 // when
219- doReturn ( Optional . of ( category )). when ( categoryRepository ). findById ( anyLong ( ));
220- doReturn ( product ). when (productService ) .findById (anyLong ());
221- doReturn ( expectedResponse ). when (categoryRepository ) .save (any (Category .class ));
197+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . of ( category ));
198+ when (productService .findById (anyLong ())). thenReturn ( product );
199+ when (categoryRepository .save (any (Category .class ))). thenReturn ( expectedResponse );
222200
223201 final CategoryResponse response = categoryService .removeProductFromCategory (category .getId (), product .getId ());
224202
@@ -236,8 +214,8 @@ void testRemoveProductThatIsNotInCategory() {
236214 final Category category = categoryTestBuilder .withDefaultValues ().build ();
237215
238216 // when
239- doReturn ( Optional . of ( category )). when ( categoryRepository ). findById ( anyLong ( ));
240- doReturn ( product ). when (productService ) .findById (anyLong ());
217+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . of ( category ));
218+ when (productService .findById (anyLong ())). thenReturn ( product );
241219 final Exception exception = assertThrows (NotFoundException .class ,
242220 () -> categoryService .removeProductFromCategory (category .getId (), product .getId ()));
243221
@@ -253,7 +231,7 @@ void testRemoveProductFromCategoryThatDoesNotExist() {
253231 final Product product = productTestBuilder .withDefaultValues ().build ();
254232
255233 // when
256- doReturn ( Optional . empty ()). when ( categoryRepository ). findById ( anyLong ());
234+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . empty ());
257235 final Exception exception = assertThrows (NotFoundException .class ,
258236 () -> categoryService .removeProductFromCategory (NONEXISTENT_CATEGORY_ID , product .getId ()));
259237
@@ -266,19 +244,13 @@ void testRemoveProductFromCategoryThatDoesNotExist() {
266244 void testRetireCategory () {
267245 // given
268246 final Category category = categoryTestBuilder .withDefaultValues ().build ();
269- final Category expectedCategory = categoryTestBuilder
270- .withDefaultValues ()
247+ final Category expectedCategory = categoryTestBuilder .withDefaultValues ()
271248 .withRetired (true )
272249 .build ();
273- final var categoryResponse = CategoryResponse .builder ()
274- .id (1L )
275- .name ("Pizzas" )
276- .description ("Italian style stone baked pizzas." )
277- .build ();
278250
279251 // when
280- doReturn ( Optional . of ( category )). when ( categoryRepository ). findById ( anyLong ( ));
281- doReturn ( expectedCategory ). when (categoryRepository ) .save (any (Category .class ));
252+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . of ( category ));
253+ when (categoryRepository .save (any (Category .class ))). thenReturn ( expectedCategory );
282254
283255 // then
284256 categoryService .retireCategory (category .getId ());
@@ -290,13 +262,12 @@ void testRetireCategory() {
290262 @ Test
291263 void testRetireCategoryAlreadyRetiredThrowsRetirementException () {
292264 // given
293- final Category retiredCategory = categoryTestBuilder
294- .withDefaultValues ()
265+ final Category retiredCategory = categoryTestBuilder .withDefaultValues ()
295266 .withRetired (true )
296267 .build ();
297268
298269 // when
299- doReturn ( Optional . of ( retiredCategory )). when ( categoryRepository ). findById ( anyLong ( ));
270+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . of ( retiredCategory ));
300271
301272 // then
302273 assertThrows (RetirementException .class , () -> categoryService .retireCategory (retiredCategory .getId ()));
@@ -306,7 +277,7 @@ void testRetireCategoryAlreadyRetiredThrowsRetirementException() {
306277 @ Test
307278 void testRetireCategoryDoesNotExistThrowsNotFoundException () {
308279 // when
309- doReturn ( Optional . empty ()). when ( categoryRepository ). findById ( anyLong ());
280+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . empty ());
310281
311282 // then
312283 assertThrows (NotFoundException .class , () -> categoryService .retireCategory (NONEXISTENT_CATEGORY_ID ));
0 commit comments