简体   繁体   English

如何用Cucumber-JVM中的一个“ Then”步骤匹配任意数量的条件?

[英]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? 在Cucumber(Java版)中,如何将所有步骤与一个“ then”功能匹配?

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! 谢谢!

Given that the list of values can grow you could map to a List with a feature file containing 鉴于值列表可以增长,您可以使用包含以下内容的功能文件映射到List

...
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...
}    

An alternative to the @Reimeus good answer, you also can match a List<Integer> in your step definition. @Reimeus好答案的替代方法,您也可以在步骤定义中匹配List<Integer>

Feature definition: 功能定义:

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

Java step code: Java步骤代码:

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

I think both choices are valid for your requirements. 我认为这两种选择均符合您的要求。 Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM