How to setup multiples step definitions for Cucumber Java features on Intellij

11,123

Solution 1

You have need to define in your cucumber options the "glue" options.

In your runner you define the where glue and features are. >>> Glue are the stepdefinitions and feature are the features.

In your case you have the stepdefinition are in the same place that features, but you have to tell it to the runner.

In your case it should be

@CucumberOptions(
    format = {" pretty", "json:target/cucumber.js"},
    features = { "src/test/"}, glue ={ "src/test/"  })

Regards,

Solution 2

Rearrange your files like this:

└── src
    └── test
        ├── java
            ├── <project>
                 ├── CheeseStepDefinition.java
                 ├── StepDefinition.java
                 └── CucumberRunner.java
        ├── resources
            ├── <project>
                 ├── cheese.feature
                 └── myfeature.feature

should be a directory names for your project. Mark the src/test/java folder as test root. Mark the src/test/recources folder as test resources root. To mark the folders: In IntelliJ, right click the folder in the Project view on the left and select Mark Directory As and select the appropriate option.

Share:
11,123
Robert
Author by

Robert

Updated on June 04, 2022

Comments

  • Robert
    Robert almost 2 years

    I am trying to create several step definitions classes for several features. It is my project structure:

    .
    ├── CucumberPOC.iml
    └── src
        └── test
            ├── CucumberRunner.java
            └── features
                ├── CheeseStepDefinition.java
                ├── StepDefinition.java
                ├── cheese.feature
                └── myfeature.feature
    

    It is the CucumberRunner.java class:

    package test;
    
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;
    import org.junit.runner.RunWith;
    
    @RunWith(Cucumber.class)
    @CucumberOptions(
            format = {" pretty", "json:target/cucumber.js"},
            features = { "src/test/" }
    )
    public class CucumberRunner {
    }
    

    There are two to step definitions classes. When I run cheese.feature I got the error:

    /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/bin/java ...
    Testing started at 9:26 AM ...
    
    Undefined step: Given  I go to google
    
    Undefined step: When  I ask for cheese
    
    Undefined step: Then  I should see many offers
    
    
    1 scenario (0 passed)
    
    
    3 steps (0 passed)
    
    
    1 Scenarios (1 undefined)
    3 Steps (3 undefined)
    0m0.000s
    
    
    You can implement missing steps with the snippets below:
    
    @Given("^I go to google$")
    public void I_go_to_google() throws Throwable {
        // Express the Regexp above with the code you wish you had
        throw new PendingException();
    }
    
    @When("^I ask for cheese$")
    public void I_ask_for_cheese() throws Throwable {
        // Express the Regexp above with the code you wish you had
        throw new PendingException();
    }
    
    @Then("^I should see many offers$")
    public void I_should_see_many_offers() throws Throwable {
        // Express the Regexp above with the code you wish you had
        throw new PendingException();
    }
    
    
    Process finished with exit code 0
    

    But the steps are defined in CheeseStepDefinition:

    package test.features;
    
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    
    public class CheeseStepDefinition {
    
        WebDriver driver = null;
    
        @Given("^I go to google$")
        public void I_go_to_google() throws Throwable {
            driver = new HtmlUnitDriver();
            driver.get("http://www.google.com");
        }
    
        @When("^I ask for cheese$")
        public void I_ask_for_cheese() throws Throwable {
            WebElement search = driver.findElement(By.name("q"));
            search.sendKeys("cheese");
            search.submit();
        }
    
        @Then("^I should see many offers$")
        public void I_should_see_many_offers() throws Throwable {
            System.out.println(driver.getTitle());
            driver.close();
        }
    }
    

    So I don't know why the cucumber java doesn't see it step definition. Do I need to do any other configuration? I run the myfeature.feature and everything is ok.

    Tools Info.

    I am using this jars:

    .
    ├── cucumber-core-1.1.5.jar
    ├── cucumber-html-0.2.3.jar
    ├── cucumber-java-1.1.5.jar
    ├── cucumber-junit-1.1.5.jar
    ├── cucumber-jvm-deps-1.0.5.jar
    ├── gherkin-2.12.1.jar
    ├── hamcrest-all-1.3.jar
    ├── jsoup-1.8.3.jar
    ├── junit-4.12.jar
    └── selenium-server-standalone-2.47.1.jar
    

    The IDE is Intellij 14.1 Community, on Mac.

    If you need any other information, let me know.