When using Cucumber, if you want to share state between multiple step definition files, you’ll need to use dependency injection (DI). There are several options: PicoContainer, Spring, OpenEJB, etc. If you’re not already using DI, then I recommend PicoContainer. Otherwise, use the one that’s already in use, because you should only have one.

First thing you’ll need to do is add the dependency to your pom file:

Next, create a new class that holds the common data. For example:

Then, in each of your step definition files that you want to use this common data, you can add a constructor that takes StepData as an argument. This is where the injection occurs. For example:

Then you can use stepData to access all of the common fields needed across your step definition classes. For example, I can split this BookStepDefinitions class into two classes:

Then in another file:

This will allow you to share the state of the fields in StepData with all of your step definition files.