0

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.

Vogel612
  • 5,620
  • 5
  • 48
  • 73
Mark
  • 3
  • 1
  • 2
  • Never tried Cucumber-Guice, but now I certainly will. A bit of DI with the features would be very helpful. Anyway, have you tried simply injecting the `GuiceFactory` or the `Injector`? – Boris the Spider May 31 '14 at 07:08

1 Answers1

1

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:

Community
  • 1
  • 1
Jan Galinski
  • 11,768
  • 8
  • 54
  • 77
  • Great thanks - I think my thinking was wrong on this. I will try it out and update – Mark Jun 02 '14 at 08:45
  • Thanks - I have been pointed in the right direction, I was going to update the question with why I was trying to do something different but realised what I was trying to do was daft (In the constructor of one object I was trying to go a getinstance basically replacing a new myService) when Iall i needed to do was @Inject!. Thank you so much – Mark Jun 02 '14 at 09:18
  • Glad I could help ... when it comes to (C)DI, my favorite rule is: never use "new" :-) – Jan Galinski Jun 02 '14 at 14:02