配置黄瓜汁 [英] Configuring cucumber-guice
问题描述
我正在尝试在步骤定义中使用DI。我有一个模块,
I am trying to use DI in my step definitions. I have a module,
public class MyModule extends AbstractModule
{
private final static MyInterface INSTANCE = new MyInterfaceImpl();
@Override
protected void configure()
{
bind(MyInterface.class).toInstance(INSTANCE);
}
}
并想将此实例注入到
public class MyStepDefs
{
private final MyInterface instance;
@Inject
public MyStepDefs(MyInterface instance)
{
this.instance = instance
}
}
我想我需要使用Cucumber-guice.properties文件配置GuiceFactory,但我真的不知道这是什么吗?目前我得到的错误是
I think I need to configure the GuiceFactory using a cucumber-guice.properties file but I don't really know what this is? At the moment the error I get is,
java.lang.NoClassDefFoundError: javax/inject/Singleton
at cucumber.runtime.java.guice.GuiceFactory$CucumberModule.configure(GuiceFactory.java:86)
也应该我正在使用提供者进行构造函数注入?
Also should I be using a Provider for constructor injection?
推荐答案
MyModule
和 MyStepDefs
类很好。 NoClassDefFoundError
是由于没有添加最新版本的Guice作为依赖项引起的。我添加了这个
The MyModule
and MyStepDefs
classes were fine. The NoClassDefFoundError
was caused by not having latest version of Guice added as a dependency. I added this,
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
到我的 POM.xml
。
cucumber-guice.properties
文件位于 src / main / resources
文件夹。该文件由 GuiceFactory
类读取,并且应包含要使用的Guice模块的属性。 EG ..
The cucumber-guice.properties
file goes in the src/main/resources
folder. This file is read by the GuiceFactory
class and should contain a property for the Guice module you want to use. EG..
guiceModule = com.felix.cucumber.MyModule
这篇关于配置黄瓜汁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!