aslakhellesoy edited this page May 25, 2011 · 6 revisions

Dependency Injection with Google Guice

If you are using Google Guice in your application, you can get your dependencies injected into your step definitions with the GuiceFactory. Enabling Guice is done with a jvm argument:


This can be passed via Ant, Maven or the Command Line.

You also have to provide the class name of the Guice configuration module you wish to use. This is also a jvm argument:


All test steps are now injected with Guice. This means that you can use @Inject exactly as you use it in your application.

public class BillingSteps {

    private BillingService billingService;

    private Transaction transaction;

    private CreateTransactionResponse response;

    @Given("^I have a transaction$")
    public void iHaveATransaction() {
        transaction = new Transaction("12345678", new BigDecimal("50.00"));

    @When("^I send the transaction to billing$")
    public void iSendTheTransactionToBilling() {
        response = billingService.sendTransactionToBilling(transaction);

    @Then("^the response should be OK$")
    public void theResponseShouldBeOK() {


Calling Steps from StepDefinitions

Follow the instructions on Calling Steps from StepDefinitions but remember to add the @Inject annotation to get the StepMother injected to your constructor.

public class CallingSteps extends Steps {
    public CallingSteps(StepMother stepMother) {

    @When("^I call another step$")
    public void iCallAnotherStep() {
        Given("it is magic"); // This will call a step defined somewhere else.

Complete Example

Check out the Guice example for a complete example.