7

Say I have:

@Given("first name is $firstName")
@Given("first name is $firstName and last name is $lastName")

The following step would be marked as ambiguous:

Given first name is John and last name is Smith

Without using quotes to surround the first parameter, how can I fix this step so it only matches the second one? Using quotes to surround both the parameters separately also has the same ambiguity issue.

Is there a limit on how long each parameter is? Are there certain characters that can't be passed in?

leppie
  • 115,091
  • 17
  • 196
  • 297

3 Answers3

8

You can solve this by using step priorities, as documented here: http://jbehave.org/reference/stable/prioritising-steps.html

Your problem would be solved by setting a higher priority to the variant with two parameters:

@Given("first name is $firstName")
@Given(value = "first name is $firstName and last name is $lastName", priority = 1)

I tried your example and with this combination, the two steps were separated.

(Edit: my initial solution had quotes for the parameters, but it works without as well)

dertseha
  • 1,086
  • 8
  • 11
1

I think this scenario can be write this way:

Given first name is John
And last name is Smith

And the steps:

@Given("first name is $firstName")
@And("last name is $lastName")

You can create the person object first an set the name and the last name in the "@Given" steps.
If you need add another property, like email, you just need create another step:

@And("the email is $email")
public addEmail(String email) {
    person.setEmail(email);
}

So this problem not gonna happen and you code will be more reusable.

Andre Piantino
  • 320
  • 1
  • 8
  • Sorry, I guess that was a bad simplification of an example of what I wanted. Let me try to rephrase it better: @Given("a person with first name $firstName") @Given("a person with first name $firstName and last name $lastName") where calling these steps would create new person objects. Then the following step would be marked as ambiguous: Given a person with first name John and last name Smith It could either parse it as creating a person with first name "John and last name Smith" or first name "John" and last name "Smith". – user1877292 Dec 05 '12 at 17:10
  • You dont need create the object in all "Given" steps, you can store the name and last name in attributes and in the "When" step create the object. I use this (similar) in [example](http://github.com/piantino/automated-traceability-example/blob/master/scrumforge-acceptance-test-jbehave/src/test/java/org/scrumforge/acceptance/test/CreateUserStoryTest.java) with this [scenarios](https://github.com/piantino/automated-traceability-example/blob/master/scrumforge-acceptance-test-jbehave/src/test/resources/stories/manage_resources/scrum_team/CreateUser.story). I think this approach is more reusable. – Andre Piantino Dec 05 '12 at 17:36
  • Yeah, I understood how I could split it into separate steps to make it work, but I wanted to see if there was a way I could keep it as one step because I want to create a lot of objects, with certain attributes set as needed. – user1877292 Dec 05 '12 at 22:03
0

What worked for me was substituting ankle brackets (<>) for the dollar sign ($), i.e.

@Given("a person with first name <firstName>")

and

@Given("a person with first name <firstName> and last name <lastName>")
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24