junit黄瓜缺少步骤错误

时间:2016-04-10 17:01:58

标签: java cucumber cucumber-junit

Java - 黄瓜示例

看起来我缺少步骤,它抱怨缺少步骤并将其视为未定义

.feature文件:

Feature: Roman Feature

  Scenario: Convert Integer to Roman Number
  Given I am on the demo page
  When I pass number 1
  Then the roman result is I


  Scenario: Convert Integer to Roman Number
  Given I am on the demo page
  When I pass number 5
  Then the roman result is V

步骤文件:

@When("^I pass number (\\d+)$")
    public void convert_numbers_to_roman(int arg1) throws Throwable {
       // convert number

    }



@Then("^the roman result is (\\d+)$")
    public void the_roman_result_is(int result) throws Throwable {
        // match result
    }

当我运行测试时

  Scenario: Convert Integer to Roman Number [90m# net/xeric/demos/roman.feature:3[0m
    [32mGiven [0m[32mI am on the demo page[0m             [90m# DemoSteps.i_am_on_the_demo_page()[0m
    [32mWhen [0m[32mI pass number [0m[32m[1m1[0m                    [90m# DemoSteps.convert_numbers_to_roman(int)[0m
    [33mThen [0m[33mthe roman result is I[0m

6场景2未定义 您可以使用以下代码段实现缺少的步骤:

@Then("^the roman result is I$")
public void the_roman_result_is_I() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

2 个答案:

答案 0 :(得分:1)

我会考虑将罗马数字作为字符串捕获,因此使用正则表达式(。*)

then步骤看起来像这样:

@Then("^the roman result is (.*)$")
public void the_roman_result_is_I(String expectedRoman) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

这与Sebastians的答案类似,但在我看来,这是一个更简单的正则表达式。它捕获任何字符串并将其作为参数传递。

您可能会在步骤中执行的断言将告诉您是否存在损坏的情况。可能更容易解决失败的断言,而不是麻烦射击失踪的步骤。

答案 1 :(得分:0)

问题在于你的正则表达式 - \\d只会匹配阿拉伯数字(Java-ese中的^the roman result is ([IVMCLX]+)$)。

你真正想要的是XkbSetDetectableAutoRepeat。这将匹配一个或多个罗马数字字符,将结果粘贴到您选择的字符串中。

相关问题