Cucumber Undefined Step, Though Defined Using IntelliJ
Asked Answered
K

8

14

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

However, the IntelliJ keeps saying that:

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.feature file:

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 MyStepdefs file.

/**
 * 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

This is my project structure: enter image description here

Kryska answered 21/5, 2017 at 21:52 Comment(1)
You need to mention the glue path using Java packages. Seems like the MyStepDefs class is without any package definition. Try moving it into the 'resteasy' package a and provide the complete package to the glue optionRamentum
H
21

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.

Hess answered 13/11, 2017 at 11:51 Comment(1)
Good thinking. I deleted the Run/Debug Configurations and re-ran. It worked! In my case, I had copied some of the cucumber steps from another scenario and used in another feature file. Your solution worked :-)Spicer
H
10

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
Hargreaves answered 29/1, 2019 at 6:26 Comment(0)
F
5

Try this:

  1. Cucumber for java plugin is installed and compatible with your intellij idea version
  2. Create and mark test\resources as Test Resources Root
  3. Put the .feature in test\resources
  4. Create the folder step_definitions in test\java and put your test code there
  5. Check that the Feature configuration contain step_definitions as Glue
  6. Check if the step_definitions code is building properly

After those checks the steps should be recognized.

Franckot answered 24/5, 2017 at 10:40 Comment(1)
Thanks, this worked for me. Seems like refactoring the stepdefs to a new folder and adding that package to the glue code solved whatever problem IntelliJ was having recognizing how the files were related.Bogusz
T
1

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.

Tension answered 29/5, 2019 at 9:28 Comment(0)
V
0

Please check glue is defined properly. If everything is alright still it throws error of undefined step then Please check the Cucumber for java plugin is compatible with current IntelliJ version. For me upgrading intelliJ version 2021 from 2018 resolved the issue.

Verruca answered 4/4, 2022 at 21:49 Comment(0)
H
0

Declare public for step definition methods.

(OP did it correctly. Check for the keyword, if your symptoms match.)

Ha answered 13/5 at 15:12 Comment(0)
G
-1

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.

enter image description here

Graphophone answered 20/9, 2019 at 16:33 Comment(0)
G
-1

Go to IntelliJ -> Preference -> Plugins -> Add "Cucumber for Java"

Glassblowing answered 26/5, 2022 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.