We are new to using cucumber with selenium to write automated test suites.

Our initial approach was to have one java class per feature file.

Now we added instance methods in each class for corresponding step definitions.

Now if we need to re-use a step definition in some other feature file we are facing problems as we can't re-use same annotation with same regex pattern with any other method of other class and neither can we use the existing step definition which is in some other class.

These methods are sharing instance variables like the reference to the driver.

Now to re-use an instance method as a step definition is also a problem because these methods are not re-usable outside the class and I can't put all step definitions inside one class.

I looked at some samples of ruby, I found that they write some sort of blocks that don't have access to shared state. They just execute steps.

So in Java should I always make static methods that will just execute steps one by one and share no state ?

3 Answers 3


My understanding is that Cucumber in Java uses step files that are POJOs and has the ability to do dependency injection via various cucumber extensions - at least, that's what it says in their documentation.

As such, unless your specs are very simple and always going to remain that way, you really want to use real instances. Static is contagious - if you make your steps static then you'll need to make everything else static, and that's not a good place to be.

Even if you don't use true DI, inheritance can also be very useful in step files, and that's generally a better way to reuse code than a spaghetti mess of statics. If all of your steps need to reference the driver, then put it in the base class, and if you want to be really cheap, use a parameterless constructor with the Service Locator pattern (which is not a great practice in general but very convenient for behaviour tests).

You say that you're concerned that step code isn't reusable outside the class, but it's not supposed to be. Steps should only be referenced by the specs themselves and should (generally) not reference each other. If there's functionality that's common to many different steps or feature groups, you should put it into a Page Object or some kind of helper. That's the reason why most of the Cucumber implementations have DI support; they want you to write your test code to approximately the same level of quality as your system code, because as it turns out, you'll likely spend 30-40% of developer hours maintaining the automated tests, so all the same best practices apply.

  • Cucumber steps are definitely supposed to be reused. To that end, it was designed to not allow multiple steps with the same text. If in one scenario you need to hit the home page and do one thing, and for another scenario hit the home and do another thing, then the hit the home page step should be only defined once.
    – Alex
    Commented Sep 15, 2014 at 16:52

I had a similar issue with a shared Selenium web driver. I configure the driver as a Spring bean with singleton scope (this is the default scope), so if you need this bean in many places just access it as a Spring bean.


Define static methods in the following scenarios only:

  1. If you are writing utility classes and they are not supposed to be changed.
  2. If the method is not using any instance variable.
  3. If any operation is not dependent on instance creation.
  4. If there is some code that can easily be shared by all the instance methods, extract that code into a static method.
  5. If you are sure that the definition of the method will never be changed or overridden. As static methods can not be overridden.
  • 1
    Do any of those situations apply to the question being asked?
    – DougM
    Commented Jan 7, 2014 at 19:24
  • 1
    The question pertains to the cucumber / selenium which has certain requirements for its environment and how tests are instrumented. The general "this is when to use static methods" doesn't take into account that setup.
    – user40980
    Commented Jan 7, 2014 at 21:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.