Discussion:
[Cucumber] DuplicateStepDefinitionException - Using same regex for @Given and @Then
(too old to reply)
Alan Lee
2017-10-03 18:27:08 UTC
Permalink
Raw Message
I am trying to build a common language for the step definitions.
I am using the same RegEx which can be used in @Given or @Then.
So I have two methods defined as below.

@Given(regex)
void method1() { }


@Then(regex)
void method2() { }

1. This fails with 'DuplicateStepDefinitionException'
2. If I remove either one then, Cucumber complains that I need to add a
step definition.

It seems weird that I cannot reuse the same regex for both @Given and @Then
although they do the same things.
Is this expected?


Thanks,
Alan
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alan Lee
2017-10-04 17:48:44 UTC
Permalink
Raw Message
Sorry. It seems I accidentally deleted a character in one of the scenario
sentences and was confused by the result.
I can use the same regex regardless of whether I defined it using @Given or
@Then.
So please ignore my original question.

Thanks,
Alan
Post by Alan Lee
I am trying to build a common language for the step definitions.
So I have two methods defined as below.
@Given(regex)
void method1() { }
@Then(regex)
void method2() { }
1. This fails with 'DuplicateStepDefinitionException'
2. If I remove either one then, Cucumber complains that I need to add a
step definition.
@Then although they do the same things.
Is this expected?
Thanks,
Alan
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Paolo Ambrosio
2017-10-07 07:15:36 UTC
Permalink
Raw Message
Post by Alan Lee
Sorry. It seems I accidentally deleted a character in one of the scenario
sentences and was confused by the result.
@Then.
Yes, technically you can reuse the same step definition for given,
when and then steps. In my experience though, you will start having
problems later on. It is better to use a certain wording (and regex)
only in one way. For example, instead of...

Given I log in
When I log in
Then I log in

...I would use...

Given I am logged in
When I log in
Then I should be logged in

What is a precondition (given), an action (when) and a post-condition
(then) should be clear even when removing the given/when/then
keywords:

- I am logged in
- I log in
- I should be logged in

This contributes to keeping the step definitions maintainable. Of
course how you differentiate the three is up to you (I like using
"should" but others don't).

Cheers,
Paolo
Post by Alan Lee
So please ignore my original question.
Thanks,
Alan
Post by Alan Lee
I am trying to build a common language for the step definitions.
So I have two methods defined as below.
@Given(regex)
void method1() { }
@Then(regex)
void method2() { }
1. This fails with 'DuplicateStepDefinitionException'
2. If I remove either one then, Cucumber complains that I need to add a
step definition.
@Then although they do the same things.
Is this expected?
Thanks,
Alan
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alan Lee
2017-10-11 17:16:45 UTC
Permalink
Raw Message
Paolo, Thanks for the tip.
Post by Alan Lee
Post by Alan Lee
Sorry. It seems I accidentally deleted a character in one of the
scenario
Post by Alan Lee
sentences and was confused by the result.
or
Post by Alan Lee
@Then.
Yes, technically you can reuse the same step definition for given,
when and then steps. In my experience though, you will start having
problems later on. It is better to use a certain wording (and regex)
only in one way. For example, instead of...
Given I log in
When I log in
Then I log in
...I would use...
Given I am logged in
When I log in
Then I should be logged in
What is a precondition (given), an action (when) and a post-condition
(then) should be clear even when removing the given/when/then
- I am logged in
- I log in
- I should be logged in
This contributes to keeping the step definitions maintainable. Of
course how you differentiate the three is up to you (I like using
"should" but others don't).
Cheers,
Paolo
Post by Alan Lee
So please ignore my original question.
Thanks,
Alan
Post by Alan Lee
I am trying to build a common language for the step definitions.
So I have two methods defined as below.
@Given(regex)
void method1() { }
@Then(regex)
void method2() { }
1. This fails with 'DuplicateStepDefinitionException'
2. If I remove either one then, Cucumber complains that I need to add a
step definition.
@Then although they do the same things.
Is this expected?
Thanks,
Alan
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google
Groups
Post by Alan Lee
"Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send
an
Post by Alan Lee
For more options, visit https://groups.google.com/d/optout.
--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...