|
9 | 9 | import java.util.Iterator;
|
10 | 10 | import java.util.Map;
|
11 | 11 | import java.util.ServiceLoader;
|
| 12 | +import java.util.stream.Collectors; |
| 13 | +import java.util.stream.Stream; |
12 | 14 |
|
13 | 15 | import static java.util.Objects.requireNonNull;
|
14 | 16 |
|
@@ -71,19 +73,26 @@ private static ObjectFactory loadSingleObjectFactoryOrDefault(ServiceLoader<Obje
|
71 | 73 | }
|
72 | 74 |
|
73 | 75 | if (objectFactories.hasNext()) {
|
74 |
| - System.out.println(getMultipleObjectFactoryLogMessage()); |
75 |
| - objectFactory = new DefaultJavaObjectFactory(); |
| 76 | + ObjectFactory extraObjectFactory = objectFactories.next(); |
| 77 | + throw new CucumberException(getMultipleObjectFactoryLogMessage(objectFactory, extraObjectFactory)); |
76 | 78 | }
|
77 | 79 | return objectFactory;
|
78 | 80 | }
|
79 | 81 |
|
80 |
| - private static String getMultipleObjectFactoryLogMessage() { |
| 82 | + private static String getMultipleObjectFactoryLogMessage(ObjectFactory... objectFactories) { |
| 83 | + String factoryNames = Stream.of(objectFactories) |
| 84 | + .map(Object::getClass) |
| 85 | + .map(Class::getName) |
| 86 | + .collect(Collectors.joining(", ")); |
| 87 | + |
81 | 88 | return "More than one Cucumber ObjectFactory was found in the classpath\n" +
|
82 | 89 | "\n" +
|
83 |
| - "You probably may have included, for instance, cucumber-spring AND cucumber-guice as part of\n" + |
84 |
| - "your dependencies. When this happens, Cucumber falls back to instantiating the\n" + |
85 |
| - "DefaultJavaObjectFactory implementation which doesn't provide IoC.\n" + |
86 |
| - "In order to enjoy IoC features, please remove the unnecessary dependencies from your classpath.\n"; |
| 90 | + "Found: " + factoryNames +"\n" + |
| 91 | + "\n" + |
| 92 | + "You may have included, for instance, cucumber-spring AND cucumber-guice as part of\n" + |
| 93 | + "your dependencies. When this happens, Cucumber can't decide which to use.\n" + |
| 94 | + "In order to enjoy dependency injection features, either remove the unnecessary dependencies" + |
| 95 | + "from your classpath or use the `cucumber.object-factory` property or `@CucumberOptions(objectFactory=...)` to select one.\n"; |
87 | 96 | }
|
88 | 97 |
|
89 | 98 | /**
|
@@ -121,7 +130,7 @@ private <T> T cacheNewInstance(Class<T> type) {
|
121 | 130 | instances.put(type, instance);
|
122 | 131 | return instance;
|
123 | 132 | } catch (NoSuchMethodException e) {
|
124 |
| - throw new CucumberException(String.format("%s doesn't have an empty constructor. If you need DI, put cucumber-picocontainer on the classpath", type), e); |
| 133 | + throw new CucumberException(String.format("%s doesn't have an empty constructor. If you need dependency injection, put cucumber-picocontainer on the classpath", type), e); |
125 | 134 | } catch (Exception e) {
|
126 | 135 | throw new CucumberException(String.format("Failed to instantiate %s", type), e);
|
127 | 136 | }
|
|
0 commit comments