07
Jun

Implementing dependency injection with Picocontainer

Implementing Dependency Injection with Picocontainer

Step 1: Maven Dependency for Picocontainer:
============================================

3.0.2



io.cucumber
cucumber-picocontainer
${cucumber.version}

Step 2: Create a class which needs to be injected via Picocontainer:
====================================================================

public class CSVUtilities
{

private CsvUtilities obj;

public CsvUtilities getObj() {
return obj;
}

public void setObj(CsvUtilities obj) {
this.obj = obj;
}

….. other functions implementations
}

Step 3: Create a PicoDependencyInjector class which extends PicoFactory:
=========================================================================

import cucumber.runtime.java.picocontainer.PicoFactory;

public class PicoDependencyInjector extends PicoFactory {

public PicoDependencyInjector() {
addClass(SharedDriver.class);
addClass(CsvUtilities.class);
}
}

Step 4: Create a class where dependency needs to be injected:
==============================================================

public class GoogleStepDef {

private GooglePage google;
private CsvUtilities obj;
private Map values;

public GoogleStepDef(GooglePage google, CsvUtilities obj) {
this.google = google;
this.obj = obj;

if(System.getProperty(“environment”).equalsIgnoreCase(“INT”)){
values = obj.readProperties(“src/test/resources/data/Integration/IntData.properties”);
System.out.println(values.toString());
}else if(System.getProperty(“environment”).equalsIgnoreCase(“STG”)){
values = obj.readProperties(“src/test/resources/data/Staging/StgData.properties”);
System.out.println(values.toString());
}
}

@Given(“^I go to google$”)
public void iGoToGoogle() throws Throwable {
google.goTo();
}

….. rest of Step definition goes here

}

Step 5: Create a TestNG Runner:
================================

import cucumber.api.CucumberOptions;
import cucumber.api.testng.CucumberFeatureWrapper;
import cucumber.api.testng.PickleEventWrapper;
import cucumber.api.testng.TestNGCucumberRunner;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

@CucumberOptions(plugin = { “pretty”,
“json:target/report/cucumber2.json” }, tags = “@myTag”, strict = true, features = {
“src/test/resources/” }, glue = { “stepDefs”, “utils” })
public class UITest {
private TestNGCucumberRunner testNGCucumberRunner;

@BeforeSuite
public void beforeSuite() {
System.out.println(“Before suite”);
}

@BeforeClass(alwaysRun = true)
public void setUpClass() throws Exception {
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
}

@Test(groups = “Cucumber”, description = “Runs Cucumber Feature”, dataProvider = “scenarios”)
public void scenario(PickleEventWrapper pickleEvent, CucumberFeatureWrapper cucumberFeature) throws Throwable {
testNGCucumberRunner.runScenario(pickleEvent.getPickleEvent());
}

@DataProvider
public Object[][] scenarios() {
Object[][] x = testNGCucumberRunner.provideScenarios();
return x;// testNGCucumberRunner.provideScenarios();
}

@AfterClass(alwaysRun = true)
public void tearDownClass() throws Exception {
testNGCucumberRunner.finish();
}

}

that’s all

Attached is sample project:

Cucumber_with_Picocontainer
Sample4Picocontainerzip