Search code examples
javaguicecucumber-jvm

cucumber-guice - using the injector


I am new to cucumber-jvm (and am returning to Java from a cucumber, Ruby background of a couple of years)

Using cucumber-guice I have successfully bound my dependencies and loaded my properties in a CucumberModule, and as such the @Inject annotation is triggering as expected.

So onto my question, in the provided GuiceFactory I can see the following code:

public <T> T More ...getInstance(Class<T> clazz) {
    return injector.getInstance(clazz);
}

So I should be able to to do a

XXXX.getInstance(myService.class);

But what is xxxx?

As far as I can tell GuiceFactory is loaded by cucumber-guice on running the tests but I have no idea what then holds the reference to it. Or am I looking at this the completely wrong way.


Solution

  • You do not create instances by hand. The Factory uses guice to create all your Step-instances.

    Whenever you used "@Inject" in your Step-class, guice will take care of the injection automatically without you interfering with the injector. You can configure the injection by providing FQN of the modules you wont to load in a cucumber-guice.properties file.

    Useful links: