1

I'm following this tutorial to set up BDD using Cucumber-JVM in a Java project. I've set up the following test file under my src/test/java folder for a Java project that I'm working on in Eclipse:

CucumberTest.java

package myPackage;

import static org.junit.Assert.fail;

import java.io.IOException;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;


@RunWith(Cucumber.class)
@CucumberOptions(features = "classpath:Feature")
public class CucumberTest {

    // error on line below 'When cannot be resolved to a type'
    @When("^the step is invoked$")
    public void myTestMethod() throws IOException {

    }   
}

I'm sure this is something simple (I'm relatively new to Cucumber for Java apps), and I believe I'm doing all of this in the correct place. How do I resolve the error? Using CTRL+SHIFT+O (organise imports) doesn't automatically import whatever it is I need, and I've looked for a relevant package I may need to import, under the cucumber.api, cucumber.api.junit and cucumber.api.junit.Cucumber namespaces, and there doesn't seem to be anything there that I should import. I've reviewed similar SO questions and haven't found any clues, as my issue is more specific.

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206

2 Answers2

0

Thanks @racraman for the hint. I was using older versions of Cucumber-JVM Maven dependencies:

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-jvm</artifactId>
        <version>1.2.5</version>
        <type>pom</type>
    </dependency>
     -->
    <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>

I replaced these for the up-to-date dependencies that use the io.cucumber <groupId>

    <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>4.2.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>4.2.2</version>
        <scope>test</scope>
    </dependency>

Now I can import the relevant annotation:

 import cucumber.api.java.en.When;
Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
0

The new pom file should be like this:

<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>7.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>7.1.0</version>
    <scope>test</scope>
</dependency>

Also use import io.cucumber.java.en.Given;

ouflak
  • 2,458
  • 10
  • 44
  • 49
Nure bhat
  • 1
  • 1