Why is @Given not repeating?

I'm all new to Cucumber (jvm) and it all seems lovely and dandy, but:

I really do not know how to have several initial conditions (from different scripts) written in different ways (elegantly) implemented by one method.

eg:

Scenario: I really am bad Given I really am inexperienced with Cucumber When I try to work Then what I produce is of poor quality Scenario: I am on the way to become good (hopefully) Given I am a noob When I learn new things And I practice Then my level improves 

Since Given I really am inexperienced with Cucumber and Given I am a cuke noob (although not semantically identical ) are close enough to be implemented in exactly the same way, I would like to be able to associate them with the same method, but

 @Given("^I really am inexperienced with Cucumber$") @Given("^I am a cuke noob$") public void checkMyLevelIsGenerallyLow() throws Throwable { // some very clever code to assess then confirm my mediocre level ... something like if(true) ... } 

But the code shown here will not compile since the cucumber.api.java.en.@Given annotation is not java.lang.annotation.@Repeatable ...

one simple solution would be to do something like

 public void checkMyLevelIsGenerallyLow() throws Throwable { // some very clever code to assess then confirm my mediocre level ... something like if(true) ... } @Given("^I really am inexperienced with Cucumber$") public void check_I_really_am_inexperienced_with_Cucumber() throws Throwable { checkMyLevelIsGenerallyLow(); } @Given("^I am a cuke noob$") public void check_I_am_a_cuke_noob() throws Throwable { checkMyLevelIsGenerallyLow(); } 

which will work very well, but will require a lot of code for simple things, and I'm sure there are other ways.

Or even when I asked myself to write down this question: “Am I just approaching this question from the right side?”, Is it that I am trying to achieve even a good idea from the point of view of BDD?

I think this is not so bad, since it is assumed that the ham keeps semantics, and the sentence structure and vocabulary are contradictory (hence the script) dependent. Nevertheless, I should be free from its implementation in any way that I like.

To wrap it all up:

  • Should @Given be @Repeatable ?
    • If so, why? Is there another way?
    • If not, what am I missing in terms of approach?
+5
source share
3 answers

Regarding multi-expressive @given

This may not be the best way to do this, but scratching my head, I thought about it:

 @Given("^I really am inexperienced with Cucumber$|^I am a cuke noob$") public void checkMyLevelIsGenerallyLow() throws Throwable { // some very clever code to assess then confirm my mediocre level ... something like if(true) ... } 

And it works! This is exactly what I was looking for, and it can even be read a little as follows:

 @Given("^I really am inexperienced with Cucumber$"+ "|^I am a cuke noob$") 

Regarding non-repeatability @given

as blalasaadri stated, @Given can be @Repeatable , but only with Java8 onwards, since @Repeatable was introduced in Java8.

Special thanks to

To Ceiling Gecko, which made me remember that the simplest and most obvious solutions are usually the best and most elegant.

+5
source

A long shot, but will not:

 @Given("^I really am inexperienced with Cucumber$") @And("^I am a cuke noob$") public void checkMyLevelIsGenerallyLow() throws Throwable { // some very clever code to assess then confirm my mediocre level ... something like if(true) ... } 

works as expected?

+4
source

One of the reasons for the impossibility of repeating annotations is that the repeated annotations are new with Java 8. Therefore, their use for the library remains problematic, since you will significantly limit the user base. (Remember, especially large corporations are slowly adopting new technologies.)

Alternatively, you can use the same Java function and similar description given; such as

 Given My cucumber level is low because I'm a cuke noob 

and

 Given My cucumber level is low because I'm inexperienced 

which can be caught

 @Given("My cucumber level is low because I'm (.*)") public void checkMyLevelIsGenerallyLow(String reason) throws Throwable { // ... } 

This will also cause the function to become the first argument. But I'm not sure that you should use the same function as the cases.

0
source

All Articles