Skip to content

Commit 3b5f23e

Browse files
committed
#20 Update javadoc for withMock() and withSpy()
1 parent a5e549b commit 3b5f23e

File tree

4 files changed

+177
-33
lines changed

4 files changed

+177
-33
lines changed

src/main/java/io/dinject/BootContext.java

Lines changed: 130 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -153,38 +153,41 @@ public BootContext withIgnoreMissingModuleDependencies() {
153153
}
154154

155155
/**
156-
* Supply a bean to the context that will be used instead of any similar bean in the context.
156+
* Supply a bean to the context that will be used instead of any
157+
* similar bean in the context.
157158
* <p>
158-
* This is typically expected to be used in tests and the bean supplied is typically a test double
159-
* or mock.
159+
* This is typically expected to be used in tests and the bean
160+
* supplied is typically a test double or mock.
160161
* </p>
161162
*
162163
* <pre>{@code
163164
*
164-
* @Test
165-
* public void someComponentTest() {
165+
* Pump pump = mock(Pump.class);
166+
* Grinder grinder = mock(Grinder.class);
166167
*
167-
* MyRedisApi mockRedis = mock(MyRedisApi.class);
168-
* MyDbApi mockDatabase = mock(MyDbApi.class);
168+
* try (BeanContext context = new BootContext()
169+
* .withBeans(pump, grinder)
170+
* .load()) {
169171
*
170-
* try (BeanContext context = new BootContext()
171-
* .withBeans(mockRedis, mockDatabase)
172-
* .load()) {
172+
* CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
173+
* coffeeMaker.makeIt();
173174
*
174-
* // built with test doubles injected ...
175-
* CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
176-
* coffeeMaker.makeIt();
175+
* Pump pump1 = context.getBean(Pump.class);
176+
* Grinder grinder1 = context.getBean(Grinder.class);
177177
*
178-
* assertThat(...
179-
* }
180-
* }
178+
* assertThat(pump1).isSameAs(pump);
179+
* assertThat(grinder1).isSameAs(grinder);
181180
*
181+
* verify(pump).pumpWater();
182+
* verify(grinder).grindBeans();
183+
* }
182184
*
183185
* }</pre>
184186
*
185187
* @param beans The bean used when injecting a dependency for this bean or the interface(s) it implements
186188
* @return This BootContext
187189
*/
190+
@SuppressWarnings("unchecked")
188191
public BootContext withBeans(Object... beans) {
189192
for (Object bean : beans) {
190193
suppliedBeans.add(new SuppliedBean(suppliedType(bean.getClass()), bean));
@@ -198,6 +201,25 @@ public BootContext withBeans(Object... beans) {
198201
* This is typically a test double often created by Mockito or similar.
199202
* </p>
200203
*
204+
* <pre>{@code
205+
*
206+
* try (BeanContext context = new BootContext()
207+
* .withBean(Pump.class, mock)
208+
* .load()) {
209+
*
210+
* Pump pump = context.getBean(Pump.class);
211+
* assertThat(pump).isSameAs(mock);
212+
*
213+
* // act
214+
* CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
215+
* coffeeMaker.makeIt();
216+
*
217+
* verify(pump).pumpSteam();
218+
* verify(pump).pumpWater();
219+
* }
220+
*
221+
* }</pre>
222+
*
201223
* @param type The dependency injection type this bean is target for
202224
* @param bean The supplied bean instance to use (typically a test mock)
203225
*/
@@ -208,31 +230,116 @@ public <D> BootContext withBean(Class<D> type, D bean) {
208230

209231
/**
210232
* Use a mockito mock when injecting this bean type.
233+
*
234+
* <pre>{@code
235+
*
236+
* try (BeanContext context = new BootContext()
237+
* .withMock(Pump.class)
238+
* .withMock(Grinder.class, grinder -> {
239+
* // setup the mock
240+
* when(grinder.grindBeans()).thenReturn("stub response");
241+
* })
242+
* .load()) {
243+
*
244+
*
245+
* CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
246+
* coffeeMaker.makeIt();
247+
*
248+
* // this is a mockito mock
249+
* Grinder grinder = context.getBean(Grinder.class);
250+
* verify(grinder).grindBeans();
251+
* }
252+
*
253+
* }</pre>
211254
*/
212255
public <D> BootContext withMock(Class<D> type) {
213-
suppliedBeans.add(new SuppliedBean<>(type, null, null));
214-
return this;
256+
return withMock(type, null);
215257
}
216258

217259
/**
218-
* Use a mockito mock when injecting this bean type additionally running setup on the mock instance.
260+
* Use a mockito mock when injecting this bean type additionally
261+
* running setup on the mock instance.
262+
*
263+
* <pre>{@code
264+
*
265+
* try (BeanContext context = new BootContext()
266+
* .withMock(Pump.class)
267+
* .withMock(Grinder.class, grinder -> {
268+
*
269+
* // setup the mock
270+
* when(grinder.grindBeans()).thenReturn("stub response");
271+
* })
272+
* .load()) {
273+
*
274+
*
275+
* CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
276+
* coffeeMaker.makeIt();
277+
*
278+
* // this is a mockito mock
279+
* Grinder grinder = context.getBean(Grinder.class);
280+
* verify(grinder).grindBeans();
281+
* }
282+
*
283+
* }</pre>
219284
*/
220285
public <D> BootContext withMock(Class<D> type, Consumer<D> consumer) {
221286
suppliedBeans.add(new SuppliedBean<>(type, null, consumer));
222287
return this;
223288
}
224289

225-
226290
/**
227291
* Use a mockito spy when injecting this bean type.
292+
*
293+
* <pre>{@code
294+
*
295+
* try (BeanContext context = new BootContext()
296+
* .withSpy(Pump.class)
297+
* .load()) {
298+
*
299+
* // setup spy here ...
300+
* Pump pump = context.getBean(Pump.class);
301+
* doNothing().when(pump).pumpSteam();
302+
*
303+
* // act
304+
* CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
305+
* coffeeMaker.makeIt();
306+
*
307+
* verify(pump).pumpWater();
308+
* verify(pump).pumpSteam();
309+
* }
310+
*
311+
* }</pre>
228312
*/
229313
public <D> BootContext withSpy(Class<D> type) {
230-
enrichBeans.add(new EnrichBean<>(type, null));
231-
return this;
314+
return withSpy(type, null);
232315
}
233316

234317
/**
235-
* Use a mockito spy when injecting this bean type additionally running setup on the spy instance.
318+
* Use a mockito spy when injecting this bean type additionally
319+
* running setup on the spy instance.
320+
*
321+
* <pre>{@code
322+
*
323+
* try (BeanContext context = new BootContext()
324+
* .withSpy(Pump.class, pump -> {
325+
* // setup the spy
326+
* doNothing().when(pump).pumpWater();
327+
* })
328+
* .load()) {
329+
*
330+
* // or setup here ...
331+
* Pump pump = context.getBean(Pump.class);
332+
* doNothing().when(pump).pumpSteam();
333+
*
334+
* // act
335+
* CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
336+
* coffeeMaker.makeIt();
337+
*
338+
* verify(pump).pumpWater();
339+
* verify(pump).pumpSteam();
340+
* }
341+
*
342+
* }</pre>
236343
*/
237344
public <D> BootContext withSpy(Class<D> type, Consumer<D> consumer) {
238345
enrichBeans.add(new EnrichBean<>(type, consumer));

src/test/java/org/example/coffee/BootContextAddTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.mockito.Mockito;
77

88
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.mockito.Mockito.verify;
910

1011
public class BootContextAddTest {
1112

@@ -76,6 +77,10 @@ public void withMockitoMock_expect_mockUsed() {
7677

7778
CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
7879
assertThat(coffeeMaker).isNotNull();
80+
coffeeMaker.makeIt();
81+
82+
verify(pump).pumpSteam();
83+
verify(pump).pumpWater();
7984
}
8085
}
8186

src/test/java/org/example/coffee/BootContext_mockitoSpyTest.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,36 @@
99

1010
import static org.assertj.core.api.Assertions.assertThat;
1111
import static org.mockito.Mockito.doNothing;
12+
import static org.mockito.Mockito.mock;
1213
import static org.mockito.Mockito.verify;
14+
import static org.mockito.Mockito.when;
1315

1416
public class BootContext_mockitoSpyTest {
1517

18+
@Test
19+
public void withBeans_asMocks() {
20+
21+
Pump pump = mock(Pump.class);
22+
Grinder grinder = mock(Grinder.class);
23+
24+
try (BeanContext context = new BootContext()
25+
.withBeans(pump, grinder)
26+
.load()) {
27+
28+
CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
29+
coffeeMaker.makeIt();
30+
31+
Pump pump1 = context.getBean(Pump.class);
32+
Grinder grinder1 = context.getBean(Grinder.class);
33+
34+
assertThat(pump1).isSameAs(pump);
35+
assertThat(grinder1).isSameAs(grinder);
36+
37+
verify(pump).pumpWater();
38+
verify(grinder).grindBeans();
39+
}
40+
}
41+
1642
@Test
1743
public void withMockitoSpy_noSetup_expect_spyUsed() {
1844

@@ -58,34 +84,41 @@ public void withMockitoSpy_expect_spyUsed() {
5884
})
5985
.load()) {
6086

87+
// or setup here ...
6188
Pump pump = context.getBean(Pump.class);
89+
doNothing().when(pump).pumpSteam();
6290

91+
// act
6392
CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
64-
assertThat(coffeeMaker).isNotNull();
6593
coffeeMaker.makeIt();
6694

6795
verify(pump).pumpWater();
96+
verify(pump).pumpSteam();
6897
}
6998
}
7099

71100
@Test
72101
public void withMockitoMock_expect_mockUsed() {
73102

74-
AtomicReference<Pump> mock = new AtomicReference<>();
103+
AtomicReference<Grinder> mock = new AtomicReference<>();
75104

76105
try (BeanContext context = new BootContext()
77-
.withMock(Grinder.class)
78-
.withMock(Pump.class, pump -> {
79-
// do something interesting to setup the mock
80-
mock.set(pump);
106+
.withMock(Pump.class)
107+
.withMock(Grinder.class, grinder -> {
108+
// setup the mock
109+
when(grinder.grindBeans()).thenReturn("stub response");
110+
mock.set(grinder);
81111
})
82112
.load()) {
83113

84-
Pump pump = context.getBean(Pump.class);
85-
assertThat(pump).isSameAs(mock.get());
114+
Grinder grinder = context.getBean(Grinder.class);
115+
assertThat(grinder).isSameAs(mock.get());
86116

87117
CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
88118
assertThat(coffeeMaker).isNotNull();
119+
coffeeMaker.makeIt();
120+
121+
verify(grinder).grindBeans();
89122
}
90123
}
91124

src/test/java/org/example/coffee/CoffeeMaker.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.example.coffee;
22

3-
import io.dinject.ContextModule;
43
import org.example.coffee.grind.Grinder;
54

65
import javax.inject.Singleton;
@@ -20,7 +19,7 @@ public CoffeeMaker(Pump pump, Grinder grinder) {
2019

2120
public String makeIt() {
2221
System.out.println("making it ...");
23-
grinder.grindBeans();
22+
System.out.println("grinder:" + grinder.grindBeans());
2423
pump.pumpWater();
2524
pump.pumpSteam();
2625
System.out.println("Done");

0 commit comments

Comments
 (0)