Search code examples
javatestingcucumber

Using multiple classes in Cucumber glue option package


I have a problem using the glue option when running Cucumber tests. Here's my test runner class:

@RunWith(Cucumber.class)
@CucumberOptions(features="src/test/java/com/xcase/tests/cucumber/features/api/APITest.feature",glue={"com.xcase.tests.cucumber.stepdefinitions.api"})
public class APIRunnerTest {

}

I run the tests along these lines:

mvn clean test -Dtest=APIRunnerTest

If I put all of my step definitions in a single class in the glue package, com.xcase.tests.cucumber.stepdefinitions.api.FirstSteps, then my tests run fine.

If I add an empty class to the com.xcase.tests.cucumber.stepdefinitions.api package, say com.xcase.tests.cucumber.stepdefinitions.api.SecondSteps,then my tests run fine. However, if I modify SecondSteps to extend FirstSteps, then my tests stop running completely! Why is that?

This is a problem for me because I'd like to put some shared step definitions and fields in a base step definition class and then have multiple classes that extend the base class. How should I do this?


Solution

  • However, if I modify SecondSteps to extend FirstSteps, then my tests stop running completely! Why is that?

    You now have two classes that declare steps. And because SecondSteps extends FirstSteps, SecondSteps will declare the same exact steps as FirstSteps. So Cucumber can't decide which steps should be run.

    This is a problem for me because I'd like to put some shared step definitions and fields in a base step definition class and then have multiple classes that extend the base class. How should I do this?

    If you want to share information between steps you should use a world object. The documentation uses ruby but after adding cucumber-pico as a dependency it works the same way in Java. For a dated tutorial check Sharing state between steps in Cucumber-JVM using PicoContainer