我发现的解决方案是使用继承自原始 Cucumber 跑步者的自定义 Junit4 跑步者并改变其createRuntime
方法。
最新的 cucumber-guice 1.2.5 使用几个阶段来创建注入器,不幸的是它使用了全局变量cucumber.runtime.Env.INSTANCE
。此变量由cucumber.properties
和填充System.getProperties
。
流程是:
- Cucumber runner 扫描可用的后端(在我的设置中是
cucumber.runtime.java.JavaBackend
)
- 可用的 JavaBackend 构造函数负载之一
ObjectFactory
(在我的设置中是 cucumber.runtime.java.guice.impl.GuiceFactory)
- GuiceFactory 通过 InjectorSourceFactory 检查
Env.INSTANCE
,它将创建自定义InjectorSource
或默认注入器
理想情况下,黄瓜应该将其在开始时创建的“RuntimeOptions”传递给后端和 InjectorSource,但不幸的是它没有传递,而是使用全局变量。像这样创建补丁并不容易,所以我的解决方案简化了这种方法,并通过阅读新注释直接在自定义运行器中创建 InjectorSource。
public class GuiceCucumberRunner extends Cucumber {
public GuiceCucumberRunner(Class<?> clazz) throws InitializationError, IOException {
super(clazz);
}
@Override
protected Runtime createRuntime(ResourceLoader resourceLoader, ClassLoader classLoader, RuntimeOptions runtimeOptions) throws InitializationError, IOException {
Runtime result = new Runtime(resourceLoader, classLoader, Arrays.asList(createGuiceBackend()), runtimeOptions);
return result;
}
private JavaBackend createGuiceBackend() {
GuiceCucumberOptions guiceCucumberOptions = getGuiceCucumberOptions();
InjectorSource injectorSource = createInjectorSource(guiceCucumberOptions.injectorSource());
ObjectFactory objectFactory = new GuiceFactory(injectorSource.getInjector());
JavaBackend result = new JavaBackend(objectFactory);
return result;
}
private GuiceCucumberOptions getGuiceCucumberOptions() {
GuiceCucumberOptions guiceCucumberOptions = getTestClass().getJavaClass().getAnnotation(GuiceCucumberOptions.class);
if (guiceCucumberOptions == null) {
String message = format("Suite class ''{0}'' is missing annotation GuiceCucumberOptions", getTestClass().getJavaClass());
throw new CucumberException(message);
}
return guiceCucumberOptions;
}
private InjectorSource createInjectorSource(Class<? extends InjectorSource> clazz) {
try {
return clazz.newInstance();
} catch (Exception e) {
String message = format("Instantiation of ''{0}'' failed. InjectorSource must have has a public zero args constructor.", clazz);
throw new InjectorSourceInstantiationFailed(message, e);
}
}
static class GuiceFactory implements ObjectFactory {
private final Injector injector;
GuiceFactory(Injector injector) {
this.injector = injector;
}
@Override
public boolean addClass(Class<?> clazz) {
return true;
}
@Override
public void start() {
injector.getInstance(ScenarioScope.class).enterScope();
}
@Override
public void stop() {
injector.getInstance(ScenarioScope.class).exitScope();
}
@Override
public <T> T getInstance(Class<T> clazz) {
return injector.getInstance(clazz);
}
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface GuiceCucumberOptions {
Class<? extends InjectorSource> injectorSource();
}
@RunWith(GuiceCucumberRunner.class)
@GuiceCucumberOptions(injectorSource = MyInjector.class)
@CucumberOptions(
...
)
public class Suite {
}
我需要复制 GuiceFactory 因为它不公开普通的构造函数(!)