【问题标题】:Cucumber .feature file not picking up Step Definition in Java classCucumber .feature 文件未在 Java 类中获取步骤定义
【发布时间】:2018-11-13 17:23:51
【问题描述】:

我正在为我的 java 项目编写一些黄瓜测试。 我的测试运行良好,但在我的 .feature 文件中出现了一个小警告。

下面,我将 .feature 文件中的一个整数传递到我在单独的 java 类中的步骤定义中

在我的 .feature 文件的以下步骤下会出现一条黄色波浪线:

Then the status code should be <StatusCode>

我收到的警告信息是:

找不到the status code should be的定义

这是我的功能文件示例表:

| StatusCode |
| 200        |

以下是我的步骤定义:

@Then("^the status code should be (\\d+)$")

这个错误使我无法 Ctrl + Clicking 'Then' 语句将我带到我的 java 类中的上述步骤定义。

有人对可能出现的问题有任何建议吗?

也许这不是您应该通过示例表传递整数的方式

【问题讨论】:

  • 所有其他步骤定义工作正常吗?
  • @LunaticJape 嗨 - 是的,我可以按 Ctrl 键并单击 .feature 文件中的其他步骤定义。真的很奇怪
  • @Then下你的方法是什么?

标签: java cucumber gherkin feature-file


【解决方案1】:

匹配步骤方法的正则表达式必须匹配步骤中的文本(字面意思)。

如果你的步骤是

Then the status code should be <StatusCode>

胶水代码中的正则表达式

@Then("^the status code should be (\\d+)$")

StatusCode 不匹配(因此您不能按住 CTRL 键并单击它)。

下面的简单例子就可以了。

Scenario Outline: outline
  Given something
  Then the status code should be <StatusCode>
  Examples:
    | StatusCode |
    | 200        |

和链接的步骤定义

@Then("^the status code should be ([^\"]*)$")
public void theStatusCodeShouldBe(String statusCode) throws Throwable {
    ...
}

edit如果您想将状态码作为Integer 传递,您可以将步骤定义的签名更改为

@Then("^the status code should be ([^\"]*)$")
public void theStatusCodeShouldBe(Integer statusCode) throws Throwable {
    ...
}

【讨论】:

  • @user9847788 您可能会更改步骤定义的签名。查看添加的示例。
猜你喜欢
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多