黄瓜与Guice - 多个guice注射器

时间:2016-02-10 12:57:48

标签: java cucumber guice cucumber-junit

我使用CucumberGuice作为DI。 我遇到过以下问题: 我有一步,即

class MyStep() {

    @Inject
    private MyService myService;

    @Given("Some acction happen")
    public void sthHappen() {
        myService.doSth();
    }
}

我已将此课程作为JUnit测试

运行
@RunWith(Cucumber.class)
@CucumberOptions(...)
public class MyTest {

}

有一个

class MyModule extends AbstractModule {
    @Override
    protected void configure() {
         bind(MyService.class).to(MyFirstService.class);     
    }
}
我的MyInjectorSource使用的

我定义了我定义guice.injector-source=MyInjectorSource;的cucumber.properties 还有一个带有场景的功能文件。 现在一切都在运作。

并且我不想在其他MyService实现中运行MyStep步骤(当然我不会重复MyStep的代码) 我用新场景和新的测试类

定义了一个新的特征文件
@RunWith(Cucumber.class)
@CucumberOptions(...)
public class MyOtherTest {

}

现在我尝试创建另一个InjectorSource,但我无法配置它。

1 个答案:

答案 0 :(得分:2)

我发现的解决方案是使用继承自原始Cucumber跑步者并更改其createRuntime方法的自定义Junit4跑步者。

最新的黄瓜guise 1.2.5使用几个阶段来创建注入器,不幸的是它使用全局变量cucumber.runtime.Env.INSTANCE。此变量由cucumber.propertiesSystem.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,因为它没有公开普通的构造函数(!)