Add step which results in a different value being passed in step definition

Viewed 27

I've the following step definition in my Cucumber test:

@When("Customer {string} connects, Negotiates, Sends DR")
public void customerConnectsNegotiatesSendsDR(String session) throws InterruptedException {
    buildsAndSendRequestToAgd(session);
    RequestMessage requestMessage=new RequestMessage("DataRequest","1");
    requestMessageBuilder.build(requestMessage).ifPresent(sender);

I want to update this so I can also have the step @When("Customer {string} connects, Negotiates, Sends DR for snapshot only"). In doing so, RequestMessage("DataRequest","1") would become RequestMessage("DataRequest","0").

I know I could update the @When("Customer {string} connects, Negotiates, Sends DR") step to take a parameter of 0 or 1 but it would require I make the update in all feature files that use it. I'm wondering if there's a simpler way where I can default the value to one but if the step @When("Customer {string} connects, Negotiates, Sends DR for snapshot only") is used it opts for 0.

Is this possible or is there an accepted way to do this?

1 Answers

I haven't tried it but regular expressions are supported in step definitions: https://cucumber.io/docs/cucumber/step-definitions/#expressions, with the capture group being passed as an argument to the stepdef method, so perhaps something like the following would work:

@When("Customer {string} connects, Negotiates, Sends DR( for snapshot only)?")
public void customerConnectsNegotiatesSendsDR(String session, String snapshotOnly) throws InterruptedException {
    ...
}
Related