Skip to content

Commit cd442d8

Browse files
Merge pull request #114 from AnswerConsulting/BENCH-269
Bench 269
2 parents ee53527 + 23b9b88 commit cd442d8

File tree

4 files changed

+53
-106
lines changed

4 files changed

+53
-106
lines changed

src/test/java/com/answerdigital/answerking/builder/AddCategoryRequestTestBuilder.java renamed to src/test/java/com/answerdigital/answerking/builder/CategoryRequestTestBuilder.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@
22

33
import com.answerdigital.answerking.request.CategoryRequest;
44

5-
public class AddCategoryRequestTestBuilder {
5+
public class CategoryRequestTestBuilder {
66

77
private String name;
88

99
private String description;
1010

11-
public AddCategoryRequestTestBuilder withDefaultValues() {
11+
public CategoryRequestTestBuilder withDefaultAddRequestValues() {
1212
this.name = "Burgers";
1313
this.description = "A selection of delicious burgers.";
1414
return this;
1515
}
1616

17-
public AddCategoryRequestTestBuilder withName(final String name) {
17+
public CategoryRequestTestBuilder withDefaultUpdateRequestValues() {
18+
this.name = "Pizzas";
19+
this.description = "Italian style stone baked pizzas.";
20+
return this;
21+
}
22+
23+
public CategoryRequestTestBuilder withName(final String name) {
1824
this.name = name;
1925
return this;
2026
}
2127

22-
public AddCategoryRequestTestBuilder withDescription(final String description) {
28+
public CategoryRequestTestBuilder withDescription(final String description) {
2329
this.description = description;
2430
return this;
2531
}

src/test/java/com/answerdigital/answerking/builder/CategoryTestBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
public class CategoryTestBuilder {
1111

12-
private Long id = 1L;
12+
private Long id;
1313

1414
private String name;
1515

@@ -27,8 +27,8 @@ public CategoryTestBuilder withDefaultValues() {
2727
this.id = 1L;
2828
this.name = "Burgers";
2929
this.description = "A selection of delicious burgers.";
30-
this.createdOn = LocalDateTime.now();
31-
this.lastUpdated = LocalDateTime.now();
30+
this.createdOn = LocalDateTime.of(2022, 1, 1, 0, 0);
31+
this.lastUpdated = LocalDateTime.of(2022, 1, 1, 0, 0);
3232
this.retired = false;
3333
this.products = new HashSet<>();
3434
return this;

src/test/java/com/answerdigital/answerking/builder/UpdateCategoryRequestTestBuilder.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/test/java/com/answerdigital/answerking/service/CategoryServiceTest.java

Lines changed: 40 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.answerdigital.answerking.service;
22

3-
import com.answerdigital.answerking.builder.AddCategoryRequestTestBuilder;
3+
import com.answerdigital.answerking.builder.CategoryRequestTestBuilder;
44
import com.answerdigital.answerking.builder.CategoryTestBuilder;
55
import com.answerdigital.answerking.builder.ProductTestBuilder;
6-
import com.answerdigital.answerking.builder.UpdateCategoryRequestTestBuilder;
76
import com.answerdigital.answerking.exception.custom.NameUnavailableException;
87
import com.answerdigital.answerking.exception.custom.ProductAlreadyPresentException;
98
import com.answerdigital.answerking.exception.custom.RetirementException;
@@ -13,10 +12,9 @@
1312
import com.answerdigital.answerking.repository.CategoryRepository;
1413
import com.answerdigital.answerking.request.CategoryRequest;
1514
import com.answerdigital.answerking.response.CategoryResponse;
16-
import org.junit.jupiter.api.AfterEach;
17-
import org.junit.jupiter.api.BeforeEach;
1815
import org.junit.jupiter.api.Test;
1916
import org.junit.jupiter.api.extension.ExtendWith;
17+
import org.mockito.InjectMocks;
2018
import org.mockito.Mock;
2119
import org.mockito.junit.jupiter.MockitoExtension;
2220

@@ -29,12 +27,13 @@
2927
import static org.mockito.ArgumentMatchers.anyLong;
3028
import static org.mockito.ArgumentMatchers.anyString;
3129
import static org.mockito.Mockito.any;
32-
import static org.mockito.Mockito.doReturn;
3330
import static org.mockito.Mockito.verify;
31+
import static org.mockito.Mockito.when;
3432

3533
@ExtendWith(MockitoExtension.class)
3634
final 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

Comments
 (0)