Search code examples
javaandroidcucumberappium

How to reuse a method from another step definition class Appium/Cucumber/Java


Trying to reuse a method in another class, initialising the class like this:

public class SettingsStepDefs {
public Scenario scenario;

@Autowired
public AndroidBase androidBase;
public GenericStepDefs genericStepDefs;


@Before
public void before(Scenario scenario) {
    this.scenario = scenario;
    genericStepDefs = new GenericStepDefs();

and then later using the method

genericStepDefs.iTapDone();

However i am getting a Null pointer exception on the line above.

The code in iTapDone() is functional, if I copy the line and use it on SettingsStepDefs, it works.

Thank you in advance.


Solution

  • You need to share an instance of GenericStepDefs between the steps. Since the variable genericStepDefs isn't set, you will get a NullPointerException.

    You seem to be using Spring since you use the annotation @Autowired. Look into sharing state with Spring for Cucumber. It is done by setting up Spring for Cucumber-JVM. The dependency

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>1.2.5</version>
    </dependency>
    

    is needed.