0

I have steps definitions, let's say ClassA and ClassB, when in ClassB i want to use variable defined in ClassA.

Background:
Given Test environment is DEV
Then We get product info

ClassA()

Environment environment;//Enum like DEV("23.556.444.55", "44.555.666.77")

@Given("^Test environment is (.*)$")
public void setEnv(String name) {
//here i should define Env var (Enum)
environment = EnumClass.getEnvironment(name)

}

ClassB() {

@Then("^Then We get product info$")
public void getProdDetails() {
//Use here "environment" value defined in ClassA
}
}

How I can reach that, i believe to introduce any static is not a good approach.

I realize that ClassB should have a dependency ClassA so as a constructor injection Class A should be passed into ClassB as parameter but how "environment" field with defined value will be injected ?

Many thanks for any hints

27P
  • 1,183
  • 16
  • 22
  • 1
    http://www.thinkcode.se/blog/2017/04/01/sharing-state-between-steps-in-cucumberjvm-using-picocontainer – M.P. Korstanje Jul 04 '19 at 17:06
  • @mpkorstanje as I understood this solution by constructor injection, is there an option with field injection? Frankly speaking do not want add cucumber-spring – 27P Jul 04 '19 at 17:43
  • And picocontainer is lack of good examples and documentation – 27P Jul 04 '19 at 19:54
  • No. Constructor injection only. But check the available DI modules in the github repo. Also the docs are looking for contributors! Feel free to send a PR! https://github.com/cucumber/docs.cucumber.io/ – M.P. Korstanje Jul 04 '19 at 20:27

1 Answers1

0

Resolved by incorporating ThreadLocal(), e.g.

public class TestExecutionContextHolder {

private TestExecutionContextHolder contextLocal = new ThreadLocal<TestExecutionContext>();

public TestExecutionContext getContext() {
      TestExecutionContext context = contextLocal.get();

     // checking if context is null then new TestExecutionContext(), contextLocal.set(context)

} 
      //add void releaseContext()
}

Then Step Def class extends some class where we getContext()

27P
  • 1,183
  • 16
  • 22