黄瓜未定义的步骤,虽然使用IntelliJ定义

时间:2017-05-21 21:52:58

标签: java intellij-idea cucumber

我正在尝试运行.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

这是我的项目结构: enter image description here

5 个答案:

答案 0 :(得分:4)

试试这个:

  1. 安装了Cucumber for java插件并与您的intellij创意版本兼容
  2. 创建 test \ resources 并将其标记为Test Resources Root
  3. .feature 放入 test \ resources
  4. test \ java 中创建 step_definitions 文件夹并将测试代码放在那里
  5. 检查Feature配置是否包含step_definitionsGlue
  6. 检查step_definitions代码是否正确构建
  7. 在检查之后,应该识别步骤。

答案 1 :(得分:4)

有时会在保存对包的旧名称的引用时发生。如果已重命名包,则应检查是否存在对旧名称的引用(Cucumber Run / Debug Configurations)并删除它们。

亲切的问候

答案 2 :(得分:2)

当上面有正确的解决方案时,我将逐步建议解决方案。

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

答案 3 :(得分:0)

仔细检查您在“想法”(配置)中是否“粘胶”,在我的情况下,我输入了错误的(从相关项目的胶不是当前的)胶时发现了此类问题。

答案 4 :(得分:-2)

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

enter image description here