Reputation: 1207
I have several tests which share Step Definitions. For example:
Scenario: test1
Given that the sky is blue
And that the sun is up
When I go outside
Then I might get a sunburn
Scenario: test2
Given that the sun is up
When I go outside
Then it will be light
Both steps "And that the sun is up" and "Given that the sun is up" are equal in their implementation.
What I would like is this:
@And("that the sun is up")
@Given("that the sun is up")
public void thatTheSunIsUp() {
// Do some fancy and sunny things.
}
Unfortunately this does not work. How can I achieve the same, without having duplicate methods for equal steps?
Upvotes: 4
Views: 4401
Reputation: 3057
Every step in cucumber is defined as a Given
, When
or Then
, but in reality it's more like:
// ENTER PSUEDOCODE
@Step("that the sun is up")
public void thatTheSunIsUp() {
// Do some fancy and sunny things.
}
The keywords are interchangeable, which allows for context as to whether it's a prerequisite (Given
) an action under test (When
) or an outcome (Then
).
Defining it as you have originally (without the duplicate @And
section), you would be able to use Given
, When
, Then
, And
, But
and *
in your feature file as the keyword, and cucumber's backend should match your step, but what you use for your definition should match up with it's intended use (as described in the previous paragraph)
Upvotes: 6
Reputation: 1095
You can't annotate the same method with the same text twice in cucumber. However, you can call a method annotated with @Given using And
in your feature file.
So either remove the @And annotation, or change the text in one of the cases.
Upvotes: 4
Reputation: 26
I don't think you can, however
Are you against using the @Repeatable function?
my understanding is you could use the @Given again with @Repeatable from Java8+.
Upvotes: 0