3

AFAICT, there's not much difference between using cucumber-glue scope and instantiating member variables in step classes other than where the instantiation code resides.

For example, using cucumber-glue scope:

@Configuration
public class MyConfiguration {
  @Bean
  @Scope("cucumber-glue")
  public MyContext myContext() {
    return new MyContext();
  }
}

@SpringBootTest
public class MySteps {
  @Autowired
  private MyContext myContext;
}

versus member variables:

@SpringBootTest
public class MySteps {
  private final MyContext myContext = new MyContext();
}

Are there other differences I'm missing?

Noel Yap
  • 18,822
  • 21
  • 92
  • 144

1 Answers1

4

When you have more then one one definition file you'll want to share some information between them. You can do this by writing to the same component.

However all regular components are singleton scoped and survive between Scenarios. This makes them unsuitable for sharing test state. It might interfere with the next scenario. Fortunately cucumber-glue scoped components (including step definitions) are recycled between scenarios. This ensures your glue code will will always be fresh.

M.P. Korstanje
  • 10,426
  • 3
  • 36
  • 58
  • 1
    I just stepped through with a debugger and, for non-static member variables, they are being instantiated for each scenario (this is consistent with the docs, IIRC), same as `cucumber-glue`-scoped beans. – Noel Yap Nov 15 '19 at 21:27
  • 1
    You can't share member variables between step definition classes unless you inject step definitions into other step definitions and access them that way. This doesn't scale well. – M.P. Korstanje Nov 16 '19 at 13:19