我正在按照本教程在Java项目中使用Cucumber-JVM设置BDD。我已经在 src/test/java
文件夹下为我在 Eclipse 中处理的 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 {
}
}
我确信这是一件简单的事情(我对 Cucumber for Java 应用程序相对较新(,我相信我在正确的地方完成了所有这些工作。如何解决该错误?使用 CTRL+SHIFT+O
(组织导入(不会自动导入我需要的任何内容,我已经在 cucumber.api
、cucumber.api.junit
和 cucumber.api.junit.Cucumber
命名空间下寻找我可能需要导入的相关包,并且似乎没有我应该导入的任何内容。我已经查看了类似的SO问题,但没有找到任何线索,因为我的问题更具体。
感谢@racraman的提示。我使用的是旧版本的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>
我将它们替换为使用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>
现在我可以导入相关注释:
import cucumber.api.java.en.When;
新的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>
同时使用导入io.cucumber.java.en.Given;