@@ -55,6 +55,8 @@ public class BootContext {
55
55
56
56
private final Set <String > includeModules = new LinkedHashSet <>();
57
57
58
+ private boolean ignoreMissingModuleDependencies ;
59
+
58
60
/**
59
61
* Create a BootContext to ultimately load and return a new BeanContext.
60
62
*
@@ -115,6 +117,7 @@ public BootContext withNoShutdownHook() {
115
117
* try (BeanContext context = new BootContext()
116
118
* .withBeans(mockEmailService)
117
119
* .withModules("coffee")
120
+ * .withIgnoreMissingModuleDependencies()
118
121
* .load()) {
119
122
*
120
123
* // built with test doubles injected ...
@@ -127,7 +130,6 @@ public BootContext withNoShutdownHook() {
127
130
*
128
131
* }</pre>
129
132
*
130
- *
131
133
* @param modules The names of modules that we want to include in dependency injection.
132
134
* @return This BootContext
133
135
*/
@@ -136,6 +138,15 @@ public BootContext withModules(String... modules) {
136
138
return this ;
137
139
}
138
140
141
+ /**
142
+ * Set this when building a BeanContext (typically for testing) and supplied beans replace module dependencies.
143
+ * This means we don't need the usual module dependencies as supplied beans are used instead.
144
+ */
145
+ public BootContext withIgnoreMissingModuleDependencies () {
146
+ this .ignoreMissingModuleDependencies = true ;
147
+ return this ;
148
+ }
149
+
139
150
/**
140
151
* Supply a bean to the context that will be used instead of any similar bean in the context.
141
152
* <p>
@@ -180,7 +191,7 @@ public BootContext withBeans(Object... beans) {
180
191
public BeanContext load () {
181
192
182
193
// sort factories by dependsOn
183
- FactoryOrder factoryOrder = new FactoryOrder (includeModules , !suppliedBeans .isEmpty ());
194
+ FactoryOrder factoryOrder = new FactoryOrder (includeModules , !suppliedBeans .isEmpty (), ignoreMissingModuleDependencies );
184
195
ServiceLoader .load (BeanContextFactory .class ).forEach (factoryOrder ::add );
185
196
186
197
Set <String > moduleNames = factoryOrder .orderFactories ();
@@ -308,16 +319,18 @@ static class FactoryOrder {
308
319
309
320
private final Set <String > includeModules ;
310
321
private final boolean suppliedBeans ;
322
+ private final boolean ignoreMissingModuleDependencies ;
311
323
312
324
private final Set <String > moduleNames = new LinkedHashSet <>();
313
325
private final List <BeanContextFactory > factories = new ArrayList <>();
314
326
private final List <FactoryState > queue = new ArrayList <>();
315
327
316
- private final Map <String ,FactoryList > providesMap = new HashMap <>();
328
+ private final Map <String , FactoryList > providesMap = new HashMap <>();
317
329
318
- FactoryOrder (Set <String > includeModules , boolean suppliedBeans ) {
330
+ FactoryOrder (Set <String > includeModules , boolean suppliedBeans , boolean ignoreMissingModuleDependencies ) {
319
331
this .includeModules = includeModules ;
320
332
this .suppliedBeans = suppliedBeans ;
333
+ this .ignoreMissingModuleDependencies = ignoreMissingModuleDependencies ;
321
334
}
322
335
323
336
void add (BeanContextFactory factory ) {
@@ -384,25 +397,26 @@ private void processQueue() {
384
397
count = processQueuedFactories ();
385
398
} while (count > 0 );
386
399
387
- if (suppliedBeans ) {
400
+ if (suppliedBeans || ignoreMissingModuleDependencies ) {
388
401
// just push everything left assuming supplied beans
389
402
// will satisfy the required dependencies
390
403
for (FactoryState factoryState : queue ) {
391
404
push (factoryState );
392
405
}
393
406
394
- } else if (!queue .isEmpty ()) {
407
+ } else if (!queue .isEmpty ()) {
395
408
StringBuilder sb = new StringBuilder ();
396
409
for (FactoryState factory : queue ) {
397
- sb .append ("module " ).append (factory .getName ()).append (" has unsatisfied dependencies - " );
410
+ sb .append ("Module [ " ).append (factory .getName ()).append ("] has unsatisfied dependencies on modules: " );
398
411
for (String depModuleName : factory .getDependsOn ()) {
399
- boolean ok = moduleNames .contains (depModuleName );
400
- String result = ( ok ) ? "ok" : "UNSATISFIED" ;
401
- sb . append ( String . format ( "depends on %s - %s" , depModuleName , result ));
412
+ if (! moduleNames .contains (depModuleName )) {
413
+ sb . append ( String . format ( " [%s]" , depModuleName )) ;
414
+ }
402
415
}
403
416
}
404
417
405
- sb .append ("- Modules loaded ok " ).append (moduleNames );
418
+ sb .append (". Modules that were loaded ok are:" ).append (moduleNames );
419
+ sb .append (". Consider using BootContext.withIgnoreMissingModuleDependencies() or BootContext.withSuppliedBeans(...)" );
406
420
throw new IllegalStateException (sb .toString ());
407
421
}
408
422
}
0 commit comments