junit黄瓜缺少步骤错误

junit cucumber missing steps error

Java - 黄瓜示例

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

.特征文件:

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();
}

问题出在您的正则表达式中 - \d 只会匹配阿拉伯数字(即 Java-ese 中的 \d)。

你真正想要的是^the roman result is ([IVMCLX]+)$。这将匹配一个或多个罗马数字字符,将结果粘贴到您选择的字符串中。

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

您的 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 的回答,但在我看来,这是一个更简单的正则表达式。它捕获任何字符串并将其作为参数传递。

您可能会在该步骤中实施的断言会告诉您是否有问题。解决失败的断言可能比解决缺失的步骤更容易。