首页 > 解决方案 > Cucumber .feature 文件未在 Java 类中获取步骤定义

问题描述

我正在为我的 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 + 单击Then ”语句将我带到我的 java 类中的上述步骤定义。

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

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

标签: javacucumbergherkinfeature-file

解决方案


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

如果你的步骤是

Then the status code should be <StatusCode>

胶水代码中的正则表达式

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

将不匹配(因此您不能 CTRL+单击它)StatusCode

以下简单示例将起作用。

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 {
    ...
}

编辑如果您想传递状态代码,Integer您可能会将步骤定义的签名更改为

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

推荐阅读