1

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!

4

2 回答 2

3

鉴于值列表可以增长,您可以映射到List具有包含

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

带黄瓜码

@Then(^the response status will be one of the following$)
public void doResponseStuff(List<String> responses){
   // use response codes...
}    
于 2016-09-08T23:14:08.257 回答
2

@Reimeus 好的答案的替代方法是,您还可以List<Integer>在步骤定义中匹配 a 。

特征定义:

...
Then the response status will be 200,302,404
...

Java步骤代码:

@When("^the response status will be (.*)$")
public void the_response_status_should_be(List<Integer> statusList) throws Throwable {
   //...
}

我认为这两种选择都符合您的要求。希望能帮助到你

于 2016-09-09T09:32:44.777 回答