0
votes

I am using Cucumber plugin in RestAssured to write my feature file and automate the REST service. Below is how my scenario looks like

Scenario Outline: Validate the elements in the GET response
    Given I have the data setup to test "<version>" and "<order>" 
    When ...
    Then the response should contain accurate data
    Examples:
      | version | order   |
      | V1      | O1      |
      | V2      | O2      |

My step definition has the below signature for the method:

@Given("^I have the data setup to test \"([^\"]*)\" and \"([^\"]*)\"$")
    public void iHaveTheDataSetupToTestAnd(String clientCharacteristicTypeCd, String clientCharacteristicDataType)

My question is I want to have another scenario like this below , where I want to leverage the same above step definition , but pass an additional parameter "special order" as optional. Can I do that or do I need to create a new step deifnition for just the below Given step ? I was thinking of method overloading / Passing Optional parameter but not sure if it works in Gherkin. Something like this

 Ex:
  Scenario Outline: Validate the elements in the GET response for special order
    Given I have the data setup to test "<version>" and "<order>"  and "<specialorder>" 
   When ...
    Then the response should contain accurate data
    Examples:
      | version | order   | specialorder
      | V1      | O1      | SO1
      | V2      | O2      | SO2

public void iHaveTheDataSetupToTestAnd(String clientCharacteristicTypeCd, String clientCharacteristicDataType, String specialOrder)
1
Do you want to avoid checking parameter is empty or not in step definition ? If you are ok checking then that is enough i guess - mihir S
I want to use the same step definition for both the above scenarios but if I am passing 2 parameters for one of the scenario and the step definition has 3 in its signature then its failing with gherkin error. - user5252179

1 Answers

3
votes

What you want is to delegate to a helper where you support both steps. Then implement two steps that don't do more than catch the call and forward it to the helper.

This will allow you to do all kinds of interesting things without having lots of logic in the step class. Each step is often just one or two lines in my case, I catch the parameters and forward them to a helper where all the interesting things in driving the system under test happens.