简体   繁体   中英

jbehave regex in step name

How can I map following different steps to single java method?

    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?

I think they are two ways to realize this with Jbehave and Java:

1.) You add a whitespace between number and (st|nd|rd|th) so Jbehave can easily recognize them:

 @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:

@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) {...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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