Search code examples
javacucumbercucumber-jvmcucumber-junitcucumber-java

How to match any number of conditions with one "Then" step in Cucumber-JVM?


In cucumber (java version), how do I match all steps with one "then" function?

For example I would like to be able to match all the following in one function:

Then the response status will be "200"
Then the response status will be "200" or "302"
Then the response status will be "200" or "302" or "404"

Do I need to write a matcher for each of the "or"s?

Is there a way that I could write one matcher for all of the cases above?

For example, how do I combine these into one function?:

@Then("^the response status should be \"([^\"]*)\"$")
public void the_response_status_should_be(String arg1) throws Throwable {
    System.out.println("** the response status should be "+arg1);
}

@Then("^the response status should be \"([^\"]*)\" or \"([^\"]*)\"$")
public void the_response_status_should_be_or(String arg1, String arg2) throws Throwable {
    System.out.println("** the response status should be "+arg1+" "+arg2);
}

@Then("^the response status should be \"([^\"]*)\" or \"([^\"]*)\" or \"([^\"]*)\"$")
public void the_response_status_should_be_or(String arg1, String arg2, String arg3) throws Throwable {
    System.out.println("** the response status should be "+arg1+" "+arg2+" "+arg3);
}

Is this possible?

Thanks!


Solution

  • Given that the list of values can grow you could map to a List with a feature file containing

    ...
    Then the response status will be one of the following
        | response status | 
        | 200             |
        | 302             |
        | 404             |
    ....
    

    with Cucumber code

    @Then(^the response status will be one of the following$)
    public void doResponseStuff(List<String> responses){
       // use response codes...
    }