1

I am trying to implement Rest Assured framework with cucumber

I am facing a weird scenario that I have defined all my step definitions of my feature file then also I am getting error as below when I run my feature file:-

Step undefined
You can implement this step and 3 other step(s) using the snippet(s) below:

@Given("I create new service by using create service API data")
public void i_create_new_service_by_using_create_service_api_data() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

and When I run the same from Junit Testrunner then I get error as below :-

INFO net.serenitybdd.rest.decorators.request.RequestSpecificationDecorated - No BaseStepListener, POST /services not registered.

In my framework I am defining basepackage containing base class file which is as below :-

public class TestBase {

    public static Properties propertyConfig = new Properties();
    public static FileInputStream fis;
    public static Response response;
    public static RequestSpecification requestSpecification;

           public static void loadPreConfigs(){
               try {
                   fis = new FileInputStream("./src/test/resources/ConfigurationURLs/config.properties");
                   try {
                       propertyConfig.load(fis);
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
               } catch (FileNotFoundException e) {
                   e.printStackTrace();
               }

               RestAssured.baseURI=propertyConfig.getProperty("BaseURI");
           }
      }

Then I have a ApiCall package which contains all class files which have request specification and respective response storing rest API calls The APICall file is given below:-

public class PostRequestCall extends TestBase {

    private static String productVal;

    public static int getProductVal() {
        return Integer.parseInt(productVal);
    }

    public static void setProductVal(String productVal) {
        PostRequestCall.productVal= productVal;
    }

    public RequestSpecification definePostRequest(){
       requestSpecification= SerenityRest.given();
       requestSpecification.contentType(ContentType.JSON);
       return requestSpecification;
    }

    public Response CreateService(String serviceName){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("name",serviceName);
       response=definePostRequest().body(jsonObject).post(propertyConfig.getProperty("createService"));
       return response;

    }
}

Then I have step file which are the class file in which I define the steps of serenity given below:

public class PostRequestSteps {

    PostRequestCall postRequestCall=new PostRequestCall();

    @Step
    public RequestSpecification setPostSpecification(){
        TestBase.requestSpecification=postRequestCall.definePostRequest();
        return TestBase.requestSpecification;
    }

    @Step
    public Response setPostRequestCall(String serviceName){
        TestBase.response=postRequestCall.CreateService(serviceName);
        return TestBase.response;
    }
}

And I have defined a package which contains all the step definition classes one such class is as below :-

public class PostRequest_StepDefinitions {

    String serviceID;

    @Steps
    PostRequestSteps postRequestSteps=new PostRequestSteps();

    @Before
    public void setUp() {
        TestBase.loadPreConfigs();
    }

    @Given("I create new service by using create service API data")
    public void i_create_new_service_by_using_create_service_api_data() {
        postRequestSteps.setPostSpecification();
    }

    @When("I provide valid name {string} for service creation")
    public void i_provide_valid_name_for_service_creation(String serviceName) {
       TestBase.response=postRequestSteps.setPostRequestCall(serviceName);
    }

    @And("I save the id of created service")
    public void i_save_the_id_of_created_service() {
      serviceID=TestBase.response.jsonPath().get("id").toString();
        PostRequestCall.setProductVal(serviceID);
    }

    @Then("I validate status code {int}")
    public void i_validate_status_code(int statusCode) {
        Assert.assertEquals(TestBase.response.getStatusCode(),statusCode);
    }

The Junit Runner file and feature files are below

Junit runner file Feature file

keshav dwivedi
  • 117
  • 4
  • 14

0 Answers0