@@ -153,38 +153,41 @@ public BootContext withIgnoreMissingModuleDependencies() {
153
153
}
154
154
155
155
/**
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.
157
158
* <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.
160
161
* </p>
161
162
*
162
163
* <pre>{@code
163
164
*
164
- * @Test
165
- * public void someComponentTest() {
165
+ * Pump pump = mock(Pump.class);
166
+ * Grinder grinder = mock(Grinder.class);
166
167
*
167
- * MyRedisApi mockRedis = mock(MyRedisApi.class);
168
- * MyDbApi mockDatabase = mock(MyDbApi.class);
168
+ * try (BeanContext context = new BootContext()
169
+ * .withBeans(pump, grinder)
170
+ * .load()) {
169
171
*
170
- * try (BeanContext context = new BootContext()
171
- * .withBeans(mockRedis, mockDatabase)
172
- * .load()) {
172
+ * CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class);
173
+ * coffeeMaker.makeIt();
173
174
*
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);
177
177
*
178
- * assertThat(...
179
- * }
180
- * }
178
+ * assertThat(pump1).isSameAs(pump);
179
+ * assertThat(grinder1).isSameAs(grinder);
181
180
*
181
+ * verify(pump).pumpWater();
182
+ * verify(grinder).grindBeans();
183
+ * }
182
184
*
183
185
* }</pre>
184
186
*
185
187
* @param beans The bean used when injecting a dependency for this bean or the interface(s) it implements
186
188
* @return This BootContext
187
189
*/
190
+ @ SuppressWarnings ("unchecked" )
188
191
public BootContext withBeans (Object ... beans ) {
189
192
for (Object bean : beans ) {
190
193
suppliedBeans .add (new SuppliedBean (suppliedType (bean .getClass ()), bean ));
@@ -198,6 +201,25 @@ public BootContext withBeans(Object... beans) {
198
201
* This is typically a test double often created by Mockito or similar.
199
202
* </p>
200
203
*
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
+ *
201
223
* @param type The dependency injection type this bean is target for
202
224
* @param bean The supplied bean instance to use (typically a test mock)
203
225
*/
@@ -208,31 +230,116 @@ public <D> BootContext withBean(Class<D> type, D bean) {
208
230
209
231
/**
210
232
* 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>
211
254
*/
212
255
public <D > BootContext withMock (Class <D > type ) {
213
- suppliedBeans .add (new SuppliedBean <>(type , null , null ));
214
- return this ;
256
+ return withMock (type , null );
215
257
}
216
258
217
259
/**
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>
219
284
*/
220
285
public <D > BootContext withMock (Class <D > type , Consumer <D > consumer ) {
221
286
suppliedBeans .add (new SuppliedBean <>(type , null , consumer ));
222
287
return this ;
223
288
}
224
289
225
-
226
290
/**
227
291
* 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>
228
312
*/
229
313
public <D > BootContext withSpy (Class <D > type ) {
230
- enrichBeans .add (new EnrichBean <>(type , null ));
231
- return this ;
314
+ return withSpy (type , null );
232
315
}
233
316
234
317
/**
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>
236
343
*/
237
344
public <D > BootContext withSpy (Class <D > type , Consumer <D > consumer ) {
238
345
enrichBeans .add (new EnrichBean <>(type , consumer ));
0 commit comments