Skip to content

Commit a684c7a

Browse files
BENCH-231 added 'withDefaultValues' to test builders
1 parent 3507363 commit a684c7a

File tree

5 files changed

+79
-37
lines changed

5 files changed

+79
-37
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
public class AddCategoryRequestTestBuilder {
66

7-
private String name = "Burgers";
8-
private String description = "A selection of delicious burgers.";
7+
private String name;
8+
private String description;
9+
10+
public AddCategoryRequestTestBuilder withDefaultValues() {
11+
this.name = "Burgers";
12+
this.description = "A selection of delicious burgers.";
13+
return this;
14+
}
915

1016
public AddCategoryRequestTestBuilder withName(String name) {
1117
this.name = name;

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@
1111
public class CategoryTestBuilder {
1212

1313
private Long id = 1L;
14-
private String name = "Burgers";
15-
private String description = "A selection of delicious burgers.";
16-
private String createdOn = getDateTimeAsString();
17-
private String lastUpdated = getDateTimeAsString();;
18-
private boolean retired = false;
19-
private Set<Product> products = new HashSet<>();
14+
private String name;
15+
private String description;
16+
private String createdOn;
17+
private String lastUpdated;
18+
private boolean retired;
19+
private Set<Product> products;
20+
21+
public CategoryTestBuilder withDefaultValues() {
22+
this.id = 1L;
23+
this.name = "Burgers";
24+
this.description = "A selection of delicious burgers.";
25+
this.createdOn = getDateTimeAsString();
26+
this.lastUpdated = getDateTimeAsString();
27+
this.retired = false;
28+
this.products = new HashSet<>();
29+
return this;
30+
}
2031

2132
public CategoryTestBuilder withId(Long id) {
2233
this.id = id;
@@ -54,7 +65,9 @@ public CategoryTestBuilder withProducts(Set<Product> products) {
5465
}
5566

5667
public CategoryTestBuilder withProduct(Product product) {
57-
this.products.add(product);
68+
Set<Product> products = new HashSet<>();
69+
products.add(product);
70+
this.products = products;
5871
return this;
5972
}
6073

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,25 @@
1010

1111
public class ProductTestBuilder {
1212

13-
private Long id = 1L;
14-
private String name = "Cheeseburger";
15-
private String description = "A beef patty with cheddar cheese.";
16-
private BigDecimal price = BigDecimal.valueOf(5.00D);
17-
private boolean retired = false;
18-
private Set<Category> categories = new HashSet<>();
19-
private Set<LineItem> lineItems = new HashSet<>();
20-
13+
private Long id;
14+
private String name;
15+
private String description;
16+
private BigDecimal price;
17+
private boolean retired;
18+
private Set<Category> categories;
19+
private Set<LineItem> lineItems;
20+
21+
public ProductTestBuilder withDefaultValues() {
22+
this.id = 1L;
23+
this.name = "Cheeseburger";
24+
this.description = "A beef patty with cheddar cheese.";
25+
this.price = BigDecimal.valueOf(5.00D);
26+
this.retired = false;
27+
this.categories = new HashSet<>();
28+
this.lineItems = new HashSet<>();
29+
return this;
30+
}
31+
2132
public ProductTestBuilder withId(Long id) {
2233
this.id = id;
2334
return this;

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
public class UpdateCategoryRequestTestBuilder {
66

7-
private String name = "Pizzas";
8-
private String description = "Italian style stone baked pizzas.";
7+
private String name;
8+
private String description;
9+
10+
public UpdateCategoryRequestTestBuilder withDefaultValues() {
11+
this.name = "Pizzas";
12+
this.description = "Italian style stone baked pizzas.";
13+
return this;
14+
}
915

1016
public UpdateCategoryRequestTestBuilder withName(String name) {
1117
this.name = name;

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ void tearDown() {
7272
@Test
7373
void testAddCategory() {
7474
// given
75-
final AddCategoryRequest addCategoryRequest = addCategoryRequestTestBuilder.build();
76-
final Category expectedResponse = categoryTestBuilder.build();
75+
final AddCategoryRequest addCategoryRequest = addCategoryRequestTestBuilder.withDefaultValues().build();
76+
final Category expectedResponse = categoryTestBuilder.withDefaultValues().build();
7777

7878
// when
7979
doReturn(false).when(categoryRepository).existsByName(anyString());
@@ -93,7 +93,7 @@ void testAddCategory() {
9393
@Test
9494
void testAddCategoryThatAlreadyExists() {
9595
// given
96-
final AddCategoryRequest addCategoryRequest = addCategoryRequestTestBuilder.build();
96+
final AddCategoryRequest addCategoryRequest = addCategoryRequestTestBuilder.withDefaultValues().build();
9797

9898
// when
9999
doReturn(true).when(categoryRepository).existsByName(anyString());
@@ -108,9 +108,10 @@ void testAddCategoryThatAlreadyExists() {
108108
@Test
109109
void testUpdateCategory() {
110110
// given
111-
final Category existingCategory = categoryTestBuilder.build();
112-
final UpdateCategoryRequest updateCategoryRequest = updateCategoryRequestTestBuilder.build();
111+
final Category existingCategory = categoryTestBuilder.withDefaultValues().build();
112+
final UpdateCategoryRequest updateCategoryRequest = updateCategoryRequestTestBuilder.withDefaultValues().build();
113113
final Category expectedResponse = categoryTestBuilder
114+
.withDefaultValues()
114115
.withName(updateCategoryRequest.name())
115116
.withDescription(updateCategoryRequest.description())
116117
.build();
@@ -132,7 +133,7 @@ void testUpdateCategory() {
132133
@Test
133134
void testUpdateCategoryThatDoesNotExist() {
134135
// given
135-
final UpdateCategoryRequest updateCategoryRequest = updateCategoryRequestTestBuilder.build();
136+
final UpdateCategoryRequest updateCategoryRequest = updateCategoryRequestTestBuilder.withDefaultValues().build();
136137

137138
// when
138139
doReturn(false).when(categoryRepository).existsByNameAndIdIsNot(anyString(), anyLong());
@@ -149,8 +150,8 @@ void testUpdateCategoryThatDoesNotExist() {
149150
@Test
150151
void testUpdateCategoryNameToCategoryThatAlreadyExists() {
151152
// given
152-
final Category existingCategory = categoryTestBuilder.build();
153-
final UpdateCategoryRequest updateCategoryRequest = updateCategoryRequestTestBuilder.build();
153+
final Category existingCategory = categoryTestBuilder.withDefaultValues().build();
154+
final UpdateCategoryRequest updateCategoryRequest = updateCategoryRequestTestBuilder.withDefaultValues().build();
154155

155156
// when
156157
doReturn(true).when(categoryRepository).existsByNameAndIdIsNot(anyString(), anyLong());
@@ -165,9 +166,10 @@ void testUpdateCategoryNameToCategoryThatAlreadyExists() {
165166
@Test
166167
void testAddProductToCategory() {
167168
// given
168-
final Product product = productTestBuilder.build();
169-
final Category category = categoryTestBuilder.build();
169+
final Product product = productTestBuilder.withDefaultValues().build();
170+
final Category category = categoryTestBuilder.withDefaultValues().build();
170171
final Category expectedResponse = categoryTestBuilder
172+
.withDefaultValues()
171173
.withProduct(product)
172174
.build();
173175

@@ -188,8 +190,9 @@ void testAddProductToCategory() {
188190
@Test
189191
void testAddProductToCategoryThatIsAlreadyInCategory() {
190192
// given
191-
final Product product = productTestBuilder.build();
193+
final Product product = productTestBuilder.withDefaultValues().build();
192194
final Category category = categoryTestBuilder
195+
.withDefaultValues()
193196
.withProduct(product)
194197
.build();
195198

@@ -208,7 +211,7 @@ void testAddProductToCategoryThatIsAlreadyInCategory() {
208211
@Test
209212
void testAddProductToCategoryThatDoesNotExist() {
210213
// given
211-
final Product product = productTestBuilder.build();
214+
final Product product = productTestBuilder.withDefaultValues().build();
212215

213216
// when
214217
doReturn(Optional.empty()).when(categoryRepository).findById(anyLong());
@@ -223,11 +226,12 @@ void testAddProductToCategoryThatDoesNotExist() {
223226
@Test
224227
void testRemoveProductFromCategory() {
225228
// given
226-
final Product product = productTestBuilder.build();
229+
final Product product = productTestBuilder.withDefaultValues().build();
227230
final Category category = categoryTestBuilder
231+
.withDefaultValues()
228232
.withProduct(product)
229233
.build();
230-
final Category expectedResponse = categoryTestBuilder.build();
234+
final Category expectedResponse = categoryTestBuilder.withDefaultValues().build();
231235

232236
// when
233237
doReturn(Optional.of(category)).when(categoryRepository).findById(anyLong());
@@ -246,8 +250,8 @@ void testRemoveProductFromCategory() {
246250
@Test
247251
void testRemoveProductThatIsNotInCategory() {
248252
// given
249-
final Product product = productTestBuilder.build();
250-
final Category category = categoryTestBuilder.build();
253+
final Product product = productTestBuilder.withDefaultValues().build();
254+
final Category category = categoryTestBuilder.withDefaultValues().build();
251255

252256
// when
253257
doReturn(Optional.of(category)).when(categoryRepository).findById(anyLong());
@@ -264,7 +268,7 @@ void testRemoveProductThatIsNotInCategory() {
264268
@Test
265269
void testRemoveProductFromCategoryThatDoesNotExist() {
266270
// given
267-
Product product = productTestBuilder.build();
271+
Product product = productTestBuilder.withDefaultValues().build();
268272

269273
// when
270274
doReturn(Optional.empty()).when(categoryRepository).findById(anyLong());
@@ -279,8 +283,9 @@ void testRemoveProductFromCategoryThatDoesNotExist() {
279283
@Test
280284
void testRetireCategory() {
281285
// given
282-
Category category = categoryTestBuilder.build();
286+
Category category = categoryTestBuilder.withDefaultValues().build();
283287
Category expectedCategory = categoryTestBuilder
288+
.withDefaultValues()
284289
.withRetired(true)
285290
.build();
286291

@@ -298,6 +303,7 @@ void testRetireCategory() {
298303
void testRetireCategoryAlreadyRetiredThrowsRetirementException() {
299304
// given
300305
Category retiredCategory = categoryTestBuilder
306+
.withDefaultValues()
301307
.withRetired(true)
302308
.build();
303309

0 commit comments

Comments
 (0)