Skip to content

Commit dbcf7a1

Browse files
JonCookmaibin
authored andcommitted
BAEL-3416 - Mockito and Fluent APIs (eugenp#8201)
* BAEL-3416 - Mockito and Fluent APIs - stage changes * staging changes * BAEL-3416 - Mockito and Fluent APIs
1 parent bb3e595 commit dbcf7a1

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.baeldung.mockito.fluentapi;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Pizza {
7+
8+
public enum PizzaSize {
9+
LARGE, MEDIUM, SMALL;
10+
}
11+
12+
private String name;
13+
private PizzaSize size;
14+
private List<String> toppings;
15+
private boolean stuffedCrust;
16+
private boolean collect;
17+
private Integer discount;
18+
19+
private Pizza(PizzaBuilder builder) {
20+
this.name = builder.name;
21+
this.size = builder.size;
22+
this.toppings = builder.toppings;
23+
this.stuffedCrust = builder.stuffedCrust;
24+
this.collect = builder.collect;
25+
this.discount = builder.discount;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public PizzaSize getSize() {
33+
return size;
34+
}
35+
36+
public List<String> getToppings() {
37+
return toppings;
38+
}
39+
40+
public boolean isStuffedCrust() {
41+
return stuffedCrust;
42+
}
43+
44+
public boolean isCollecting() {
45+
return collect;
46+
}
47+
48+
public Integer getDiscount() {
49+
return discount;
50+
}
51+
52+
public static class PizzaBuilder {
53+
private String name;
54+
private PizzaSize size;
55+
56+
private List<String> toppings;
57+
private boolean stuffedCrust;
58+
private boolean collect;
59+
private Integer discount = null;
60+
61+
public PizzaBuilder() {
62+
}
63+
64+
public PizzaBuilder name(String name) {
65+
this.name = name;
66+
return this;
67+
}
68+
69+
public PizzaBuilder size(PizzaSize size) {
70+
this.size = size;
71+
return this;
72+
}
73+
74+
public PizzaBuilder withExtraTopping(String extraTopping) {
75+
if (this.toppings == null) {
76+
toppings = new ArrayList<>();
77+
}
78+
this.toppings.add(extraTopping);
79+
return this;
80+
}
81+
82+
public PizzaBuilder withStuffedCrust(boolean stuffedCrust) {
83+
this.stuffedCrust = stuffedCrust;
84+
return this;
85+
}
86+
87+
public PizzaBuilder willCollect(boolean collect) {
88+
this.collect = collect;
89+
return this;
90+
}
91+
92+
public PizzaBuilder applyDiscount(Integer discount) {
93+
this.discount = discount;
94+
return this;
95+
}
96+
97+
public Pizza build() {
98+
return new Pizza(this);
99+
}
100+
}
101+
102+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.mockito.fluentapi;
2+
3+
import com.baeldung.mockito.fluentapi.Pizza.PizzaSize;
4+
5+
public class PizzaService {
6+
7+
private Pizza.PizzaBuilder builder;
8+
9+
public PizzaService(Pizza.PizzaBuilder builder) {
10+
this.builder = builder;
11+
}
12+
13+
public Pizza orderHouseSpecial() {
14+
return builder.name("Special")
15+
.size(PizzaSize.LARGE)
16+
.withExtraTopping("Mushrooms")
17+
.withStuffedCrust(true)
18+
.withExtraTopping("Chilli")
19+
.willCollect(true)
20+
.applyDiscount(20)
21+
.build();
22+
}
23+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.baeldung.mockito.fluentapi;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.ArgumentMatchers.anyBoolean;
6+
import static org.mockito.ArgumentMatchers.anyInt;
7+
import static org.mockito.ArgumentMatchers.anyString;
8+
import static org.mockito.Mockito.verify;
9+
import static org.mockito.Mockito.when;
10+
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.mockito.Answers;
14+
import org.mockito.ArgumentCaptor;
15+
import org.mockito.Captor;
16+
import org.mockito.Mock;
17+
import org.mockito.Mockito;
18+
import org.mockito.MockitoAnnotations;
19+
20+
import com.baeldung.mockito.fluentapi.Pizza.PizzaBuilder;
21+
import com.baeldung.mockito.fluentapi.Pizza.PizzaSize;
22+
23+
public class PizzaServiceUnitTest {
24+
25+
@Mock
26+
private Pizza expectedPizza;
27+
28+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
29+
private PizzaBuilder anotherbuilder;
30+
31+
@Captor
32+
private ArgumentCaptor<String> stringCaptor;
33+
@Captor
34+
private ArgumentCaptor<Pizza.PizzaSize> sizeCaptor;
35+
36+
@Before
37+
public void setup() {
38+
MockitoAnnotations.initMocks(this);
39+
}
40+
41+
@Test
42+
public void givenTraditonalMocking_whenServiceInvoked_thenPizzaIsBuilt() {
43+
PizzaBuilder nameBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
44+
PizzaBuilder sizeBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
45+
PizzaBuilder firstToppingBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
46+
PizzaBuilder secondToppingBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
47+
PizzaBuilder stuffedBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
48+
PizzaBuilder willCollectBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
49+
PizzaBuilder discountBuilder = Mockito.mock(Pizza.PizzaBuilder.class);
50+
51+
PizzaBuilder builder = Mockito.mock(Pizza.PizzaBuilder.class);
52+
when(builder.name(anyString())).thenReturn(nameBuilder);
53+
when(nameBuilder.size(any(Pizza.PizzaSize.class))).thenReturn(sizeBuilder);
54+
when(sizeBuilder.withExtraTopping(anyString())).thenReturn(firstToppingBuilder);
55+
when(firstToppingBuilder.withStuffedCrust(anyBoolean())).thenReturn(stuffedBuilder);
56+
when(stuffedBuilder.withExtraTopping(anyString())).thenReturn(secondToppingBuilder);
57+
when(secondToppingBuilder.willCollect(anyBoolean())).thenReturn(willCollectBuilder);
58+
when(willCollectBuilder.applyDiscount(anyInt())).thenReturn(discountBuilder);
59+
when(discountBuilder.build()).thenReturn(expectedPizza);
60+
61+
PizzaService service = new PizzaService(builder);
62+
assertEquals("Expected Pizza", expectedPizza, service.orderHouseSpecial());
63+
64+
verify(builder).name(stringCaptor.capture());
65+
assertEquals("Pizza name: ", "Special", stringCaptor.getValue());
66+
67+
verify(nameBuilder).size(sizeCaptor.capture());
68+
assertEquals("Pizza size: ", PizzaSize.LARGE, sizeCaptor.getValue());
69+
70+
}
71+
72+
@Test
73+
public void givenDeepStubs_whenServiceInvoked_thenPizzaIsBuilt() {
74+
Mockito.when(anotherbuilder.name(anyString())
75+
.size(any(Pizza.PizzaSize.class))
76+
.withExtraTopping(anyString())
77+
.withStuffedCrust(anyBoolean())
78+
.withExtraTopping(anyString())
79+
.willCollect(anyBoolean())
80+
.applyDiscount(anyInt())
81+
.build())
82+
.thenReturn(expectedPizza);
83+
84+
PizzaService service = new PizzaService(anotherbuilder);
85+
assertEquals("Expected Pizza", expectedPizza, service.orderHouseSpecial());
86+
}
87+
88+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.mockito.fluentapi;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import org.junit.Test;
8+
9+
import com.baeldung.mockito.fluentapi.Pizza.PizzaSize;
10+
11+
public class PizzaUnitTest {
12+
13+
@Test
14+
public void givenFluentPizzaApi_whenBuilt_thenPizzaHasCorrectAttributes() {
15+
Pizza pizza = new Pizza.PizzaBuilder()
16+
.name("Margherita")
17+
.size(PizzaSize.LARGE)
18+
.withExtraTopping("Mushroom")
19+
.withStuffedCrust(false)
20+
.willCollect(true)
21+
.applyDiscount(20)
22+
.build();
23+
24+
assertEquals("Pizza name: ", "Margherita", pizza.getName());
25+
assertEquals("Pizza size: ", PizzaSize.LARGE, pizza.getSize());
26+
assertEquals("Extra toppings: ", "Mushroom", pizza.getToppings()
27+
.get(0));
28+
assertFalse("Has stuffed crust: ", pizza.isStuffedCrust());
29+
assertTrue("Will collect: ", pizza.isCollecting());
30+
assertEquals("Discounts: ", Integer.valueOf(20), pizza.getDiscount());
31+
}
32+
}

0 commit comments

Comments
 (0)