【问题标题】:Cucumber Undefined Step, Though Defined Using IntelliJ黄瓜未定义步骤,虽然使用 IntelliJ 定义
【发布时间】:2017-05-21 21:52:57
【问题描述】:

我正在尝试运行一个 .feature 文件来测试一个简单的 RESTEasy Web 应用程序:https://github.com/dashorst/jaxrs-quickstart-resteasy

但是,IntelliJ 一直在说:

Undefined step: Given  I am an invalid username

Undefined step: When  I perform a hello request with null username

Undefined step: Then  I receive a http response code of 400

Undefined step: When  I perform a hello request with empty username

Undefined step: Then  I receive a http response code of 400

You can implement missing steps with the snippets below:

@Given("^I am an invalid username$")
public void I_am_an_invalid_username() throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}
// similar results...

以下是我的hello.feature 文件:

Feature: hello

#-------------------------------------
#next block is got the GET entry point
#-------------------------------------

#verify that with malformed input hello fails 
Scenario: invalid username should fail
Given I am an invalid username
When I perform a hello request with null username
Then I receive a http response code of 400
When I perform a hello request with empty username
Then I receive a http response code of 400

#verify that you can get correct hello response with valid username. This is the 'everything works fine' path.
Scenario: User can get correct hello response with valid username
Given I am a valid username
When I perform a hello request with valid username
Then I receive a http response code of 200

import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
import cucumber.runtime.PendingException;

我使用 IntelliJ 选项“生成步骤”并获得了 MyStepdefs 文件。

/**
 * Created by z on 5/21/17.
 */
public class MyStepdefs {
    @Given("^I am a valid username$")
    public void iAmAValidUsername() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @Given("^I am an invalid username$")
    public void iAmAnInvalidUsername() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @When("^I perform a hello request with null username$")
    public void iPerformAHelloRequestWithNullUsername() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @Then("^I receive a http response code of (\\d+)$")
    public void iReceiveAHttpResponseCodeOf(int arg0) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @When("^I perform a hello request with empty username$")
    public void iPerformAHelloRequestWithEmptyUsername() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    @When("^I perform a hello request with valid username$")
    public void iPerformAHelloRequestWithValidUsername() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
}

我曾尝试为每个场景指定粘合路径,但它不起作用。我不确定发生了什么。感谢您的建议!

我已经研究了几个现有的问题,但没有一个有帮助:

Cucumber: undefined step, although step should be defined

Cucumber JVM undefined step

这是我的项目结构:

【问题讨论】:

  • 您需要提及使用 Java 包的粘合路径。似乎 MyStepDefs 类没有任何包定义。尝试将其移动到“resteasy”包中,并将完整的包提供给粘合选项

标签: java intellij-idea cucumber


【解决方案1】:

有时会在保存对包旧名称的引用时发生。如果包已重命名,您应该检查是否有对旧名称(Cucumber 运行/调试配置)的引用并删除它们。

亲切的问候

【讨论】:

  • 好主意。我删除了运行/调试配置并重新运行。有效!就我而言,我从另一个场景中复制了一些黄瓜步骤,并在另一个功能文件中使用。您的解决方案有效:-)
【解决方案2】:

当上面有正确的解决方案时,我会逐步推荐一个解决方案。

  1. 在 IntelliJ 中通过“运行 > 编辑配置...”
  2. 从“Cucumber java”列表中选择您的 Cucumber java 类
  3. 用你的黄瓜实现所在的路径填写“Glue”。例如,“com.blabla.test.steps”
  4. 点击“应用”按钮
  5. 尝试运行/调试您的黄瓜功能文件

【讨论】:

    【解决方案3】:

    试试这个:

    1. Cucumber for java 插件已安装并与您的 intellij idea 版本兼容
    2. 创建并标记 test\resourcesTest Resources Root
    3. .feature 放入 test\resources
    4. test\java 中创建文件夹 step_definitions 并将您的测试代码放在那里
    5. 检查Feature 配置是否包含step_definitions 作为Glue
    6. 检查 step_definitions 代码是否正确构建

    在这些检查之后,应该可以识别这些步骤。

    【讨论】:

    • 谢谢,这对我有用。似乎将 stepdefs 重构到一个新文件夹并将该包添加到胶水代码中解决了 IntelliJ 在识别文件如何相关时遇到的任何问题。
    【解决方案4】:

    仔细检查你在想法(配置)中的“胶水”,在我的情况下,我在输入不正确的胶水(来自依赖项目的胶水不是当前的)胶水时发现了这样的问题。

    【讨论】:

      【解决方案5】:

      我犯了同样的错误并找到了几个不同的答案。但同样的错误总是存在。 所以我最终找到了解决方案。 我使用的是 cucumbe.api 依赖项,但在我的 Steps 类中我正在导入 io.cucumber,所以找不到我的步骤。 然后删除导入并选择正确的导入。

      enter image description here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多