Ar3*_*r3s 5 java bdd annotations cucumber-jvm
我对Cucumber(jvm)都很陌生,这一切看起来都很精致,但是:
我真的不知道如何通过单一方法实现以各种方式(优雅地)编写的多个初始条件(来自各种场景).
例如:
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
Run Code Online (Sandbox Code Playgroud)
由于Given I really am inexperienced with Cucumber和Given I am a cuke noob(虽然在语义上不相同)足够接近我以完全相同的方式实现,我希望能够将它们链接到相同的方法,但
@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) ...
}
Run Code Online (Sandbox Code Playgroud)
但是上面提到的代码不会编译,因为cucumber.api.java.en.@Given注释不是java.lang.annotation.@Repeatable......
一个简单的解决方案就是做类似的事情
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();
}
Run Code Online (Sandbox Code Playgroud)
这将工作得很好,但需要大量的代码来处理简单的事情,我很确定还有其他方法.
或者甚至,当我问自己写下这个问题时,"我只是从右侧接近这个问题吗?",我正试图在BDD方面实现一个好主意吗?
我认为这并不是一件坏事,因为小黄瓜应该保持语义和句子结构,词汇选择依赖于上下文(因此场景).然而,我应该以任何我喜欢的方式自由地实施它.
@Given是@Repeatable?
它可能不是最好的方式,但我抓住了我的想法:
@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) ...
}
Run Code Online (Sandbox Code Playgroud)
它的工作原理!这正是我所寻找的,甚至可以像这样更具可读性:
@Given("^I really am inexperienced with Cucumber$"+
"|^I am a cuke noob$")
Run Code Online (Sandbox Code Playgroud)
正如blalasaadri所说,@Given可能@Repeatable只是来自Java8,并且自@RepeatableJava8引入以来.
致Ceiling Gecko让我记住,最简单,最明显的解决方案通常是最好和最优雅的.