My purpose is to produce EntityManager and Logger SFL4J implementations for my cucumber guice unit tests.
I've created the following resource producer
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());
}
}
Now I guess I should bind those classes using my implementation of AbstractModule so: public class ServiceModule extends AbstractModule { @Override protected void configure() { bind( EntityManager.class ).to(?); // ... (further bindings) } }
I have no idea how to realize this. I've also tried by expliciting it:
bind( EntityManager.class ).to(Persistence.createEntityManagerFactory("h2-test").createEntityManager());
But it still fails to compile.
Any suggestions ?