简体   繁体   English

Cucumber-JVM - @When 无法解析为类型

[英]Cucumber-JVM - @When cannot be resolved to a type

I'm following this tutorial to set up BDD using Cucumber-JVM in a Java project.我正在按照本教程在 Java 项目中使用 Cucumber-JVM 设置 BDD。 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:我已经在src/test/java文件夹下为我在 Eclipse 中处理的 Java 项目设置了以下测试文件:

CucumberTest.java黄瓜测试.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.我确信这很简单(我对用于 Java 应用程序的 Cucumber 相对较新),而且我相信我在正确的地方做所有这些。 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.使用CTRL+SHIFT+O (组织导入)不会自动导入我需要的任何东西,而且我已经寻找了一个我可能需要导入的相关包,在cucumber.apicucumber.api.junitcucumber.api.junit.Cucumber命名空间,那里似乎没有任何我应该导入的东西。 I've reviewed similar SO questions and haven't found any clues, as my issue is more specific.我已经查看了类似的 SO 问题,但没有找到任何线索,因为我的问题更具体。

Thanks @racraman for the hint. 感谢@racraman的提示。 I was using older versions of Cucumber-JVM Maven dependencies: 我正在使用旧版本的Cucumber-JVM Maven依赖项:

    <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> 我将它们替换为使用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;

The new pom file should be like this:新的 pom 文件应该是这样的:

<!-- 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;也使用 import io.cucumber.java.en.Given;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM