1

I am currently building a framework to test a Rest-API endpoint. As I am planning to write a lot of test cases, I decided to organize the project to allow me to reuse common Step Definition methods.

The structure is as follows;

FunctionalTest
    com.example.steps
        -- AbstractEndpointSteps.java
        -- SimpleSearchSteps.java
    com.example.stepdefinitions
        -- CommonStepDefinition.java
        -- SimpleSearchStepDefinition.java`

However when I try to call SimpleSearchSteps.java methods I get a NullPointerException

CommonStepDefinition Code

package com.example.functionaltest.features.stepdefinitions;

import net.thucydides.core.annotations.Steps;

import com.example.functionaltest.steps.AbstractEndpointSteps;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;


public class CommonStepDefinition {

    @Steps
    private AbstractEndpointSteps endpointSteps;

    @Given("^a base uri \"([^\"]*)\" and base path \"([^\"]*)\"$")
    public void aBaseUriAndBasePath(String baseURI, String basePath) {
        endpointSteps.givenBasepath(baseURI, basePath);
    }

    @When("^country is \"([^\"]*)\"$")
    public void countryIs(String country) 
    {
        endpointSteps.whenCountry(country);
    }

    @Then("^the status code is (\\d+)$")
    public void theStatusCodeIs(int statusCode) {
        endpointSteps.executeRequest();
        endpointSteps.thenTheStatusCodeIs200(statusCode);
    }

}

SimpleSearchStepDefinition.java

package com.example.functionaltest.features.stepdefinitions;

import net.thucydides.core.annotations.Steps;
import com.example.functionaltest.steps.EndpointSteps;
import cucumber.api.java.en.When;


public class SimpleSearchStepDefinition {

    @Steps
    private SimpleSearchSteps searchSteps;



    @When("^what is \"([^\"]*)\"$")
    public void whatIs(String what) {
        searchSteps.whenWhatIsGiven(what);
    }

}
philip
  • 484
  • 9
  • 26

3 Answers3

1

Looks like you are missing holder class for Cucumber annotation, something like this you should have so that cucumber knows and identified that steps and features of yours:

@RunWith(Cucumber.class)
@CucumberOptions(
    glue = {"com.example.functionaltest.features.steps"},
    features = {"classpath:functionaltest/features"}
)
public class FunctionalTest {
}

Note that, in your src/test/resources you should have functionaltest/features folder with your .feature files according to this sample, you can ofc, change it by your design

BigGinDaHouse
  • 1,282
  • 9
  • 19
0

Can you take a look at Karate it is exactly what you are trying to build ! Since you are used to Cucumber, here are a few things that Karate provides as enhancements (being based on Cucumber-JVM)

  • built-in step-definitions, no need to write Java code
  • re-use *.feature files and call them from other scripts
  • dynamic data-driven testing
  • parallel-execution of tests
  • ability to run some routines only once per feature

Disclaimer: I am the dev.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
0

I solved this issue by using a static instance of RequestSpecBuilder in the AbstractEndpointSteps instead of RequestSpecification.

Therefore, I was able to avoid duplication of StepDefinitions and NPE issues altogether

philip
  • 484
  • 9
  • 26