Guice - 来自资源生产者的绑定 类
Guice - Binding classes from resource producer
我的目的是为我的 Cucumber Guice 单元测试生成 EntityManager 和 Logger SFL4J 实现。
我创建了以下资源生产者
public class MockResourcesWeb {
private static final Logger logger = LoggerFactory.getLogger(MockResourcesWeb.class);
private EntityManager entityManager;
@PostConstruct
void init() {
try {
entityManager = Persistence.createEntityManagerFactory("h2-test").createEntityManager();
} catch (Exception e) {
logger.warn("Failed to initialize persistence unit", e);
}
}
@Produces
public EntityManager getEntityManager(InjectionPoint injectionPoint) {
return entityManager;
}
@Produces
public Logger produceLog(InjectionPoint injectionPoint) {
return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}
现在我想我应该使用 AbstractModule 的实现来绑定那些 classes,所以:
public class ServiceModule 扩展 AbstractModule {
@覆盖
受保护的无效配置(){
绑定(EntityManager.class).to(?);
// ...(进一步绑定)
}
}
我不知道如何实现这一点。我也试过明确它:
bind( EntityManager.class ).to(Persistence.createEntityManagerFactory("h2-test").createEntityManager());
但是还是编译失败
有什么建议吗?
My purpose is to produce EntityManager and Logger SFL4J
implementations for my cucumber guice unit tests.
使用 guice 注入获得 EntityManager
和 SFL4J
的实例
你可以使用cucumber-guice
如下
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-guice</artifactId>
<version>6.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
然后在 Global
class
import com.google.inject.Singleton;
import io.cucumber.guice.ScenarioScoped;
// Scenario scoped it is used to show Guice
// what will be the shared classes/variables and instantiate them only in here
@ScenarioScoped
//@Singleton
public class Global {
public EntityManager entityManager = new EntityManager();
}
创建一个BasePage
class并在构造函数中传递Global
//import classes
public class BasePage {
protected Global global;
public BasePage(Global global) {
this.global = global;
}
}
现在在你的步骤定义中(单元测试或其他黄瓜测试)
import com.google.inject.Inject;
//import other classes as necessary
public class StepDefs extends BasePage {
public static EntityManager entityMgr;
@Inject
public StepDefs(Global global) {
super(global);
StepDefs.entityMgr = global.entityManager
}
//use entityMgr as needed
//note that a new instance of entityMgr will be created for each scenario in feature file`
// to create an instance that will last for all tests use @singleton in Global class . ex : for sfl4j
}
我的目的是为我的 Cucumber Guice 单元测试生成 EntityManager 和 Logger SFL4J 实现。
我创建了以下资源生产者
public class MockResourcesWeb {
private static final Logger logger = LoggerFactory.getLogger(MockResourcesWeb.class);
private EntityManager entityManager;
@PostConstruct
void init() {
try {
entityManager = Persistence.createEntityManagerFactory("h2-test").createEntityManager();
} catch (Exception e) {
logger.warn("Failed to initialize persistence unit", e);
}
}
@Produces
public EntityManager getEntityManager(InjectionPoint injectionPoint) {
return entityManager;
}
@Produces
public Logger produceLog(InjectionPoint injectionPoint) {
return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}
现在我想我应该使用 AbstractModule 的实现来绑定那些 classes,所以: public class ServiceModule 扩展 AbstractModule { @覆盖 受保护的无效配置(){ 绑定(EntityManager.class).to(?); // ...(进一步绑定) } }
我不知道如何实现这一点。我也试过明确它:
bind( EntityManager.class ).to(Persistence.createEntityManagerFactory("h2-test").createEntityManager());
但是还是编译失败
有什么建议吗?
My purpose is to produce EntityManager and Logger SFL4J implementations for my cucumber guice unit tests.
使用 guice 注入获得 EntityManager
和 SFL4J
的实例
你可以使用cucumber-guice
如下
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-guice</artifactId>
<version>6.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
然后在 Global
class
import com.google.inject.Singleton;
import io.cucumber.guice.ScenarioScoped;
// Scenario scoped it is used to show Guice
// what will be the shared classes/variables and instantiate them only in here
@ScenarioScoped
//@Singleton
public class Global {
public EntityManager entityManager = new EntityManager();
}
创建一个BasePage
class并在构造函数中传递Global
//import classes
public class BasePage {
protected Global global;
public BasePage(Global global) {
this.global = global;
}
}
现在在你的步骤定义中(单元测试或其他黄瓜测试)
import com.google.inject.Inject;
//import other classes as necessary
public class StepDefs extends BasePage {
public static EntityManager entityMgr;
@Inject
public StepDefs(Global global) {
super(global);
StepDefs.entityMgr = global.entityManager
}
//use entityMgr as needed
//note that a new instance of entityMgr will be created for each scenario in feature file`
// to create an instance that will last for all tests use @singleton in Global class . ex : for sfl4j
}