Search code examples
cucumber-jvm

picocontainer for singleton DI


I'm trying to use picocontainer for DI but still getting my shared object instantiated several times instead of being automatically managed as a singleton. Here's an example to illustrate. Classes ASteps and BSteps receive a SharedObject instance through their constructors. I expected it to be managed as a singleton by picocontainer: instantiated only once, as per the Cucumber docs. Instead, I see it gets instantiated once for ASteps and once for BSteps:

Running my.domain.CucumberRunTest
 INFO [main] (CucumberHooks.java:15) - Executing before()
 INFO [main] (SharedObject.java:11) - SharedObject - instantiated
 INFO [main] (ASteps.java:21) - Executing a_step_one()
 INFO [main] (ASteps.java:26) - Executing a_step_two()
 INFO [main] (ASteps.java:31) - Executing a_step_three()
 INFO [main] (CucumberHooks.java:20) - Executing after()
 INFO [main] (CucumberHooks.java:15) - Executing before()
 INFO [main] (SharedObject.java:11) - SharedObject - instantiated
 INFO [main] (BSteps.java:23) - Executing b_step_one()
 INFO [main] (BSteps.java:28) - Executing b_step_two()
 INFO [main] (BSteps.java:33) - Executing b_step_three()
 INFO [main] (CucumberHooks.java:20) - Executing after()

What am I doing wrong? Here is the code:

package my.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class ASteps {

final Logger log = LoggerFactory.getLogger(getClass());
SharedObject sharedObject;

public ASteps(SharedObject sharedObject) {
this.sharedObject = sharedObject;
}

@Given("^A step one$")
public void a_step_one() {
log.info("Executing a_step_one()");
}

@When("^A step two$")
public void a_step_two() {
log.info("Executing a_step_two()");
}

@Then("^A step three$")
public void a_step_three() {
log.info("Executing a_step_three()");
}
}

************************

package my.domain;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class BSteps {

final Logger log = LoggerFactory.getLogger(getClass());
SharedObject sharedObject;

public BSteps(SharedObject sharedObject) {
this.sharedObject = sharedObject;
}

@Given("^B step one$")
public void b_step_one() {
log.info("Executing b_step_one()");
}

@When("^B step two$")
public void b_step_two() {
log.info("Executing b_step_two()");
}

@Then("^B step three$")
public void b_step_three() {
log.info("Executing b_step_three()");
}
}

************************

package my.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cucumber.api.java.After;
import cucumber.api.java.Before;

public class CucumberHooks {

final Logger log = LoggerFactory.getLogger(getClass());

@Before
public void before() {
log.info("Executing before()");
}

@After
public void after() {
log.info("Executing after()");

}
}

*********************

package my.domain;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
public class CucumberRunTest {

}

**********************

package my.domain;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SharedObject {

final Logger log = LoggerFactory.getLogger(getClass());

public SharedObject() {
log.info("SharedObject - instantiated");
}
}

Solution

  • The cache of the pico container is reset between scenarios, when the world is disposed of at the end of the scenario.

    This is very much per design to avoid state leaking between scenarios and make them isolated so the order of the tests doesn't influence the results.

    If you do wish to maintain state between scenario A and scenario B, you either need to handle SharedObject yourself, outside of the pico container, or you can make the dependency between these two scenarios explicit – for example by using a Background.