DI and Cucumber

You don’t need to use a DI container when you use Cucumber. When you use Cucumber without one of the DI integrations, it manages the creation of all your hooks and step definitions itself. Cucumber creates fresh instances of each step definition or hook class for each scenario. Of course that means that these classes need to have a default constructor; otherwise Cucumber won’t know how to create them. This makes it hard to share state safely between several step definition classes.

DI can make some of your everyday work less tedious and error prone. As soon as you add one of the DI integrations, the DI container takes over the responsibility for creating the hooks and step definitions. All the DI containers can also inject ...

Get The Cucumber for Java Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.