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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44102161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:56:30  来源:igfitidea点击:

Cucumber Undefined Step, Though Defined Using IntelliJ

javaintellij-ideacucumber

提问by OMGPOP

I'm trying to run a .feature file to test a simple RESTEasy web application: https://github.com/dashorst/jaxrs-quickstart-resteasy.

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

However, the IntelliJ keeps saying that:

然而,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...

Below is my hello.featurefile:

下面是我的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;

I used IntelliJ option "generate steps" and got the MyStepdefsfile.

我使用 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();
    }
}

I had tried to specify glue path for each scenario, but it doesn't work. I'm not sure what went run. Thanks for any advice!

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

I have looked into several existing questions but none of them helps:

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

Cucumber: undefined step, although step should be defined

黄瓜:未定义的步骤,虽然应该定义步骤

Cucumber JVM undefined step

黄瓜JVM未定义的步骤

This is my project structure: enter image description here

这是我的项目结构: 在此处输入图片说明

回答by Mr Cas

Sometimes it happens when there are saved references to the old names of the packages. If the packages have been renamed you should check if there are references to the old names (Cucumber Run/Debug Configurations) and delete them.

有时,当保存了对包的旧名称的引用时会发生这种情况。如果包已重命名,您应该检查是否有对旧名称(Cucumber Run/Debug Configurations)的引用并删除它们。

Kind regards

亲切的问候

回答by Giulio Caccin

Try this:

试试这个:

  1. Cucumber for java plugin is installed and compatible with your intellij idea version
  2. Create and mark test\resourcesas Test Resources Root
  3. Put the .featurein test\resources
  4. Create the folder step_definitionsin test\javaand put your test code there
  5. Check that the Featureconfiguration contain step_definitionsas Glue
  6. Check if the step_definitions code is building properly
  1. Cucumber for java 插件已安装并与您的intellij idea 版本兼容
  2. 创建并将测试\资源标记为Test Resources Root
  3. .feature放入test\resources
  4. test\java 中创建文件夹step_definitions并将您的测试代码放在那里
  5. 检查Feature配置是否包含step_definitions作为Glue
  6. 检查 step_definitions 代码是否正确构建

After those checks the steps should be recognized.

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

回答by Furkan

I'll recommend a solution step by step while there is correct solutions above.

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

  1. Go through "Run > Edit Configurations..." within your IntelliJ
  2. Select your cucumber java class from "Cucumber java" list
  3. Fill the "Glue" with the path where your cucumber implementation is in. For example, "com.blabla.test.steps"
  4. Click "Apply" button
  5. Try to run/debug your cucumber feature file
  1. 通过 IntelliJ 中的“运行 > 编辑配置...”
  2. 从“Cucumber java”列表中选择你的Cucumber java类
  3. 用黄瓜实现所在的路径填充“胶水”。例如,“com.blabla.test.steps”
  4. 点击“应用”按钮
  5. 尝试运行/调试您的黄瓜特征文件

回答by AlexPes

Double check you 'glue' in the Idea (Configuration), in my case I caught such issue while entering incorrect (glue from dependent project not current) glue.

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

回答by user12096618

I made the same mistake and found several different answers. But the same mistake always remained. So I ended up finding the solution. I was using a cucumbe.api dependency, but in my Steps class I was importing io.cucumber, so my steps could not be found. Then delete the import and select the correct import.

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

enter image description here

在此处输入图片说明