Java:包cucumber.api.junit不存在。

4
我一直在尝试让这个工作起来。
我想要设置一个测试运行器类,就像这样
然而我得到了这个错误:

Error:(3, 26) java: package cucumber.api.junit does not exist
Error:(10, 10) java: cannot find symbol
symbol: class Cucumber

该类如下所示:
package nl.prvgld.sigma.aanvraagtitels.testutil;

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
public class RunFeature {
}

Pom.xml的样式如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
  <modelVersion>4.0.0</modelVersion>
  <groupId>nl.prvgld.sigma.aanvraagtitels</groupId>
  <artifactId>Aanvraagtitels</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Aanvraagtitels</name>
  <url>http://maven.apache.org</url>
  <dependencies>
      <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>3.2.0</version>
      </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java8</artifactId>
        <version>1.2.5</version>
    </dependency>
      <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core -->
      <dependency>
          <groupId>info.cukes</groupId>
          <artifactId>cucumber-core</artifactId>
          <version>1.2.5</version>
          <scope>test</scope>
      </dependency>
      <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
      <dependency>
          <groupId>info.cukes</groupId>
          <artifactId>cucumber-junit</artifactId>
          <version>1.2.5</version>
      </dependency>

      <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
      <dependency>
          <groupId>com.microsoft.sqlserver</groupId>
          <artifactId>mssql-jdbc</artifactId>
          <version>6.1.0.jre8</version>
      </dependency>
      <dependency>
          <groupId>com.smartbear.readyapi.testserver.cucumber</groupId>
          <artifactId>testserver-cucumber-core</artifactId>
          <version>1.0.0</version>
          <scope>test</scope>
      </dependency>


  </dependencies>
</project>

所有的类都在Test文件夹下。

我看了一些相关问题并尝试了解决方案,比如从Pom中删除test,确保Pom中有junit、cucumber-junit、cucumber-java8和cucumber-core。

我正在使用Intellij。

非常感谢任何指引!

祝好!

3个回答

7

我不知道您是否已经解决了问题,但是您可以通过从pom.xml文件中删除范围标签来快速解决它。对于所有存在问题的依赖项执行此操作,应该可以正常工作。

祝好, HH


谢谢。这个问题已经通过上面的解决方案得到解决了。我记得尝试过你的想法,但在那种情况下并没有起作用。不知何故,那个项目出了问题。但无论如何,问题已经解决了。还是谢谢你的回答! - Chai
你也可以考虑将导入 "cucumber.api.junit" 类的代码放到测试文件夹中。这是我在使用 TypeRegistryConfigurer 时遇到的问题:https://cucumber.io/docs/cucumber/configuration/#recommended-location - user3054986

5
通过检查,一切看起来都很好。但显然,它不能按照你的预期工作。
在使用IDEA编译和Maven编译时,是否会出现相同的错误?
首先,我建议从Cucumber团队克隆一个入门项目并让其正常工作。稍后再扩展它以包含所需的内容会更容易。请克隆https://github.com/cucumber/cucumber-java-skeleton 并使用Maven进行构建。
mvn clean install

预计可以开箱即用。

有了可行的解决方案,采取小步骤朝着您真正想要的项目前进。


谢谢Thomas。我已经有大约20个类(模拟UI和一些辅助工具),所以按照你的建议去做可能是最好的想法,但这需要一些工作。我希望能找到一个更快的解决方案。我会先尝试使用maven编译。我还没有这样做过。mvn clean install是同样的吗? - Chai
编辑:我发了一条评论,但它没有意义...我找到了答案...待续! - Chai
好的,所以:我已经下载了它。将其更改为Java8。当我看到它仍然可以工作时,根据此答案重命名了项目。然后我把所有的包和类放在了它们应该在的位置,现在它就像魔法一样工作了!不知何故,颜色也不同了,所以我怀疑我的项目最初设置有问题。无论如何,非常感谢! - Chai
2
我很高兴听到它对你有用。我认为你遇到了Gall定律的情况。更多信息请参见:http://www.thinkcode.se/blog/2015/02/28/galls-law - Thomas Sundberg

1

更改导入。

替换: import cucumber.api.junit.Cucumber;

使用 import io.cucumber.junit.Cucumber;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接