简体   繁体   English

步骤名称中的jbehave正则表达式

[英]jbehave regex in step name

How can I map following different steps to single java method? 如何将以下不同步骤映射到单个java方法?

    Then 1st message should be ...
    Then 2nd message should be ...
    Then 3rd message should be ...
    Then 15th message should be ...

    @Then("$ord(st|nd|rd|th) message should be ...")
    public void assertMessage(int ord) {...}

Why (st|nd|rd|th) is not working? 为什么(st|nd|rd|th)不起作用?

I think they are two ways to realize this with Jbehave and Java: 我认为,这是使用Jbehave和Java实现这一目标的两种方法:

1.) You add a whitespace between number and (st|nd|rd|th) so Jbehave can easily recognize them: 1.)在数字和(st | nd | rd | th)之间添加空格,以便Jbehave可以轻松识别它们:

 @Then("$var {st|nd|rd|th} message should be ...")
 public void assertMessage(int var) {
    System.out.println("VAR:"+var);
 }

so your story is: 所以你的故事是:

 Then 1 st message should be ...
 Then 2 nd message should be ...
 Then 3 rd message should be ...
 Then 15 th message should be ...

2.) You read the number and the annotation and perform a substring function assuming that you always want to remove the last two chars: 2)假设您一直想删除最后两个字符,您将读取数字和注释并执行子字符串功能:

@Then("$var message should be ...")
public void assertMessage(String var) {
   int nr = Integer.parseInt(var.substring(0, var.length()-2));
   System.out.println("VAR:"+nr);
}

so you can keep your story like: 这样您就可以保持故事如:

 Then 1st message should be ...
 Then 2nd message should be ...
 Then 3rd message should be ...
 Then 15th message should be ...

If you rewrite your story just a little, then this becomes pretty easy. 如果您只重写故事,这将变得非常容易。 It doesn't really require any special regex in the step name. 在步骤名称中,它实际上并不需要任何特殊的正则表达式。

Then message 1 should be ...
Then message 2 should be ...
Then message 3 should be ...
Then message 15 should be ...

@Then("message $ord should be ...")
public void assertMessage(int ord) {...}

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

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