1212import com .answerdigital .answerking .repository .CategoryRepository ;
1313import com .answerdigital .answerking .request .CategoryRequest ;
1414import com .answerdigital .answerking .response .CategoryResponse ;
15- import org .junit .jupiter .api .AfterEach ;
16- import org .junit .jupiter .api .BeforeEach ;
1715import org .junit .jupiter .api .Test ;
1816import org .junit .jupiter .api .extension .ExtendWith ;
1917import org .mockito .InjectMocks ;
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 {
@@ -50,7 +48,7 @@ final class CategoryServiceTest {
5048
5149 private final ProductTestBuilder productTestBuilder ;
5250
53- private static final Long NONEXISTENT_CATEGORY_ID = 2L ;
51+ private static final Long NONEXISTENT_CATEGORY_ID = 10L ;
5452
5553 private CategoryServiceTest () {
5654 categoryTestBuilder = new CategoryTestBuilder ();
@@ -65,8 +63,8 @@ void testAddCategory() {
6563 final Category expectedResponse = categoryTestBuilder .withDefaultValues ().build ();
6664
6765 // when
68- doReturn ( false ). when (categoryRepository ) .existsByName (anyString ());
69- doReturn ( expectedResponse ). when (categoryRepository ) .save (any (Category .class ));
66+ when (categoryRepository .existsByName (anyString ())). thenReturn ( false );
67+ when (categoryRepository .save (any (Category .class ))). thenReturn ( expectedResponse );
7068 final CategoryResponse categoryResponse = categoryService .addCategory (addCategoryRequest );
7169
7270 // then
@@ -85,7 +83,7 @@ void testAddCategoryThatAlreadyExists() {
8583 final CategoryRequest addCategoryRequest = categoryRequestTestBuilder .withDefaultAddRequestValues ().build ();
8684
8785 // when
88- doReturn ( true ). when (categoryRepository ) .existsByName (anyString ());
86+ when (categoryRepository .existsByName (anyString ())). thenReturn ( true );
8987 final Exception exception = assertThrows (NameUnavailableException .class ,
9088 () -> categoryService .addCategory (addCategoryRequest ));
9189
@@ -103,16 +101,11 @@ void testUpdateCategory() {
103101 .withName (updateCategoryRequest .name ())
104102 .withDescription (updateCategoryRequest .description ())
105103 .build ();
106- final var categoryResponse = CategoryResponse .builder ()
107- .id (1L )
108- .name ("Pizzas" )
109- .description ("Italian style stone baked pizzas." )
110- .build ();
111104
112105 // when
113- doReturn ( false ). when (categoryRepository ) .existsByNameAndIdIsNot (anyString (), anyLong ());
114- doReturn ( Optional . of ( existingCategory )). when ( categoryRepository ). findById ( anyLong ( ));
115- 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 );
116109
117110 final CategoryResponse response = categoryService .updateCategory (updateCategoryRequest , existingCategory .getId ());
118111
@@ -129,8 +122,8 @@ void testUpdateCategoryThatDoesNotExist() {
129122 final CategoryRequest updateCategoryRequest = categoryRequestTestBuilder .withDefaultUpdateRequestValues ().build ();
130123
131124 // when
132- doReturn ( false ). when (categoryRepository ) .existsByNameAndIdIsNot (anyString (), anyLong ());
133- doReturn ( Optional . empty ()). when ( categoryRepository ). findById ( anyLong ());
125+ when (categoryRepository .existsByNameAndIdIsNot (anyString (), anyLong ())). thenReturn ( false );
126+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . empty ());
134127 final Exception exception = assertThrows (NotFoundException .class ,
135128 () -> categoryService .updateCategory (updateCategoryRequest , NONEXISTENT_CATEGORY_ID ));
136129
@@ -147,7 +140,7 @@ void testUpdateCategoryNameToCategoryThatAlreadyExists() {
147140 final CategoryRequest updateCategoryRequest = categoryRequestTestBuilder .withDefaultUpdateRequestValues ().build ();
148141
149142 // when
150- doReturn ( true ). when (categoryRepository ) .existsByNameAndIdIsNot (anyString (), anyLong ());
143+ when (categoryRepository .existsByNameAndIdIsNot (anyString (), anyLong ())). thenReturn ( true );
151144 final Exception exception = assertThrows (NameUnavailableException .class ,
152145 () -> categoryService .updateCategory (updateCategoryRequest , existingCategory .getId ()));
153146
@@ -165,8 +158,8 @@ void testAddProductToCategoryThatIsAlreadyInCategory() {
165158 .build ();
166159
167160 // when
168- doReturn ( Optional . of ( category )). when ( categoryRepository ). findById ( anyLong ( ));
169- doReturn ( product ). when (productService ) .findById (anyLong ());
161+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . of ( category ));
162+ when (productService .findById (anyLong ())). thenReturn ( product );
170163 final Exception exception = assertThrows (ProductAlreadyPresentException .class ,
171164 () -> categoryService .addProductToCategory (category .getId (), product .getId ()));
172165
@@ -182,7 +175,7 @@ void testAddProductToCategoryThatDoesNotExist() {
182175 final Product product = productTestBuilder .withDefaultValues ().build ();
183176
184177 // when
185- doReturn ( Optional . empty ()). when ( categoryRepository ). findById ( anyLong ());
178+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . empty ());
186179 final Exception exception = assertThrows (NotFoundException .class ,
187180 () -> categoryService .addProductToCategory (NONEXISTENT_CATEGORY_ID , product .getId ()));
188181
@@ -201,9 +194,9 @@ void testRemoveProductFromCategory() {
201194 final Category expectedResponse = categoryTestBuilder .withDefaultValues ().build ();
202195
203196 // when
204- doReturn ( Optional . of ( category )). when ( categoryRepository ). findById ( anyLong ( ));
205- doReturn ( product ). when (productService ) .findById (anyLong ());
206- 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 );
207200
208201 final CategoryResponse response = categoryService .removeProductFromCategory (category .getId (), product .getId ());
209202
@@ -221,8 +214,8 @@ void testRemoveProductThatIsNotInCategory() {
221214 final Category category = categoryTestBuilder .withDefaultValues ().build ();
222215
223216 // when
224- doReturn ( Optional . of ( category )). when ( categoryRepository ). findById ( anyLong ( ));
225- doReturn ( product ). when (productService ) .findById (anyLong ());
217+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . of ( category ));
218+ when (productService .findById (anyLong ())). thenReturn ( product );
226219 final Exception exception = assertThrows (NotFoundException .class ,
227220 () -> categoryService .removeProductFromCategory (category .getId (), product .getId ()));
228221
@@ -238,7 +231,7 @@ void testRemoveProductFromCategoryThatDoesNotExist() {
238231 final Product product = productTestBuilder .withDefaultValues ().build ();
239232
240233 // when
241- doReturn ( Optional . empty ()). when ( categoryRepository ). findById ( anyLong ());
234+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . empty ());
242235 final Exception exception = assertThrows (NotFoundException .class ,
243236 () -> categoryService .removeProductFromCategory (NONEXISTENT_CATEGORY_ID , product .getId ()));
244237
@@ -254,15 +247,10 @@ void testRetireCategory() {
254247 final Category expectedCategory = categoryTestBuilder .withDefaultValues ()
255248 .withRetired (true )
256249 .build ();
257- final var categoryResponse = CategoryResponse .builder ()
258- .id (1L )
259- .name ("Pizzas" )
260- .description ("Italian style stone baked pizzas." )
261- .build ();
262250
263251 // when
264- doReturn ( Optional . of ( category )). when ( categoryRepository ). findById ( anyLong ( ));
265- 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 );
266254
267255 // then
268256 categoryService .retireCategory (category .getId ());
@@ -279,7 +267,7 @@ void testRetireCategoryAlreadyRetiredThrowsRetirementException() {
279267 .build ();
280268
281269 // when
282- doReturn ( Optional . of ( retiredCategory )). when ( categoryRepository ). findById ( anyLong ( ));
270+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . of ( retiredCategory ));
283271
284272 // then
285273 assertThrows (RetirementException .class , () -> categoryService .retireCategory (retiredCategory .getId ()));
@@ -289,7 +277,7 @@ void testRetireCategoryAlreadyRetiredThrowsRetirementException() {
289277 @ Test
290278 void testRetireCategoryDoesNotExistThrowsNotFoundException () {
291279 // when
292- doReturn ( Optional . empty ()). when ( categoryRepository ). findById ( anyLong ());
280+ when ( categoryRepository . findById ( anyLong ())). thenReturn ( Optional . empty ());
293281
294282 // then
295283 assertThrows (NotFoundException .class , () -> categoryService .retireCategory (NONEXISTENT_CATEGORY_ID ));
0 commit comments