Cucumber 未定义步骤,尽管使用 IntelliJ 定义了

问题描述 投票:0回答:8

我正在尝试运行 .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:未定义步骤,尽管应该定义步骤

Cucumber JVM 未定义步骤

这是我的项目结构:

java intellij-idea cucumber
8个回答
19
投票

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


9
投票

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

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

5
投票

试试这个:

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

经过这些检查后,应该可以识别步骤。


1
投票

在创意(配置)中仔细检查“粘合”,就我而言,我在输入不正确的粘合(来自依赖项目的粘合不是当前的)粘合时发现了此类问题。


0
投票

请检查胶水是否定义正确。如果一切正常,仍然会抛出未定义步骤的错误,那么请检查 Cucumber for java 插件是否与当前的 IntelliJ 版本兼容。对我来说,从 2018 年升级 IntelliJ 版本 2021 解决了这个问题。


-1
投票

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

enter image description here


-1
投票

转到 IntelliJ -> 首选项 -> 插件 -> 添加“Cucumber for Java”


-1
投票

删除黄瓜运行/调试对我也有用!谢谢

© www.soinside.com 2019 - 2024. All rights reserved.