【问题标题】:Cucumber cannot be resolved to a typeCucumber 无法解析为类型
【发布时间】:2018-12-11 14:11:31
【问题描述】:

我创建了一个 RunTest 类来使用 Cucumber 和 JUnit 运行我的测试场景。要在之前运行测试,我需要将 RunWith 类 (@RunWith) 导入到我的项目中,并将 Cucumber.class 作为参数传递。然后,类的 RunWith 参数识别更多传递给它的参数,不。 eclipse显示消息:

此行有多个标记 - 类无法解析为类型。 - Cucumber 无法解析为一个类型。 - 注解@RunWith 必须定义属性值

我正在使用 Maven 来组织我的 JARS 文件。 以下是代码和错误屏幕。

我的 POM.XML

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <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-java</artifactId>
        <version>1.2.5</version>
    </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>

    <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core -->
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-core</artifactId>
        <version>1.2.5</version>
    </dependency>

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

</dependencies>

我的图书馆

【问题讨论】:

  • 您尝试过导入 Cucumber 吗?
  • 确实,尝试添加import cucumber.junit.Cucumber;。
  • 是的。我试过了,但eclipse显示消息:无法解决import cucumber.junit。我想我的项目中缺少一些 JAR 文件。
  • 在您的pom.xml 中,您有依赖关系info.cukes:cucumber-jvm:pom:1.2.5,它不在您的任何屏幕截图的依赖关系列表中。如果你在命令行上运行mvn compile 会发生什么?我用你的 pom.xml 和 RunTest.java 创建了一个小项目,它编译得很好。

标签: java eclipse selenium cucumber


【解决方案1】:

导入cucumber.api.junit.Cucumber 类。看来您正在使用 Eclipse IDE,因此您可以使用 Ctrl+Shift+o(字母 'o' 不为零)快捷方式导入类。

(Ctrl+Shift+O 是“组织导入”,将添加所有缺失的导入,删除所有未使用的导入,并对所有导入进行排序)。该命令还可以在 Source > Organize Imports 下找到。

【讨论】:

  • 转到项目属性->Java 构建路径。在库选项卡中,检查“cucumber-junit-1.2.5.jar”是否在 Maven 依赖项中。如果没有,请添加它。
  • 你好尼基尔·英戈尔。已添加依赖项。我用依赖的形象更新了我的问题。往上看。
  • 嗨卡卡西,我认为你缺少一些依赖的罐子。在toolsqa.com/cucumber/download-cucumber-jvm-eclipse查看“选项3”
  • 它为我创造了奇迹!
  • 它对我也有魔力。 Ctrl+Shift+O
【解决方案2】:

只需执行 Ctrl+Shift+T 并键入 RunWith 检查 jar 是否在解决 maven 依赖项后正确导入到您的工作空间中。

【讨论】:

    【解决方案3】:

    我从以下依赖项中删除了我的 POM.xml 文件:

    <!-- 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>
    

    我添加了依赖:

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

    我已经完成了导入。

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题。我在 Eclipse 中使用 Maven 项目。我的问题似乎是我使用的 JRE 版本。我的 Maven 项目都有一个旧的 J2SE-1.5,我在 Eclipse 中切换到 1.8,问题就消失了。希望这会有所帮助!

      【讨论】:

        【解决方案5】:

        如上所述,添加以下依赖项解决了我的问题:

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

        【讨论】:

          【解决方案6】:

          步骤1:鼠标悬停在错误显示上,类黄瓜无法解析为类型。 所以,手动编写import语句,错误就会消失。

          导入黄瓜.api.junit.Cucumber;

          第 2 步:从 pom.xml 文件中删除 cucumber-JUnit 依赖项的范围。

          【讨论】:

            【解决方案7】:

            从“mvnrepository”下载了所有最新的 jar maven 依赖项。这对我有用。

            【讨论】:

            • 它解决了这个特殊问题吗?
            【解决方案8】:

            请添加此依赖项并删除旧的。 它对我有用。

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

            【讨论】:

              【解决方案9】:

              我在 IntelliJ idea 中也遇到了同样的问题,当我签入导入的库时,它就像下面的屏幕截图一样。

              所以我从 pom 文件中的导入中删除了范围标签。然后它起作用了。

              之前

              <dependency>
                  <groupId>info.cukes</groupId>
                  <artifactId>cucumber-junit</artifactId>
                  <version>1.2.5</version>
                  <scope>test</scope>
              </dependency>
              

              修复

              <dependency>
                  <groupId>info.cukes</groupId>
                  <artifactId>cucumber-junit</artifactId>
                  <version>1.2.5</version>
              </dependency>
              

              现在一切正常

              【讨论】:

                【解决方案10】:

                对我来说,通过删除 .m2/repository 解决了同样的问题,重新启动 IDEA,依赖项如下:

                ${cucumber.version} = 4.8.0
                
                    <dependency>
                        <groupId>io.cucumber</groupId>
                        <artifactId>cucumber-junit</artifactId>
                        <version>${cucumber.version}</version>
                    </dependency>
                
                    <dependency>
                        <groupId>io.cucumber</groupId>
                        <artifactId>cucumber-junit</artifactId>
                        <version>${cucumber.version}</version>
                    </dependency>
                
                    <dependency>
                        <groupId>io.cucumber</groupId>
                        <artifactId>cucumber-junit</artifactId>
                        <version>${cucumber.version}</version>
                    </dependency>
                

                希望,它也适合你。

                【讨论】:

                  【解决方案11】:

                  我也遇到了同样的问题。我刚刚从 cucumber-junit 依赖项中删除了作用域部分,它对我来说效果很好。

                  【讨论】:

                    【解决方案12】:

                    即使您可能在 pom.xml

                    中有 cucumber-junit

                    确认在 Maven Dependencies 下你必须有 cucumber-junit

                    我有 1.2.6,但它不在 Maven 依赖项下,从答案中的一个 cmets 得到这个。

                      <dependency>
                      <groupId>info.cukes</groupId>
                      <artifactId>cucumber-junit</artifactId>
                       <version>1.2.5</version>
                       <scope>test</scope>
                       </dependency>
                    

                    【讨论】:

                      【解决方案13】:

                      我已经删除了测试标签并且它起作用了。

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

                      【讨论】:

                        【解决方案14】:

                        我也遇到过同样的问题。尝试了多种方法,但对我有帮助的是这个
                        import cucumber.api.junit.Cucumber;

                        【讨论】:

                          【解决方案15】:

                          我变了:

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

                          收件人:

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

                          我的依赖列表:

                          <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
                          <dependency>
                              <groupId>io.cucumber</groupId>
                              <artifactId>cucumber-junit</artifactId>
                              <version>6.2.0</version>
                              <scope>test</scope>
                          </dependency>
                          
                          <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm-deps -->
                          <dependency>
                              <groupId>io.cucumber</groupId>
                              <artifactId>cucumber-jvm-deps</artifactId>
                              <version>1.0.6</version>
                              <scope>provided</scope>
                          </dependency>
                            
                             <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
                          <dependency>
                              <groupId>io.cucumber</groupId>
                              <artifactId>cucumber-java</artifactId>
                              <version>6.2.0</version>
                          </dependency>
                            <!-- https://mvnrepository.com/artifact/junit/junit -->
                          <dependency>
                              <groupId>junit</groupId>
                              <artifactId>junit</artifactId>
                              <version>4.13</version>
                              <scope>test</scope>
                          </dependency>
                          

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 2019-12-26
                            • 2015-10-22
                            • 1970-01-01
                            • 2017-03-28
                            • 2021-08-28
                            • 2013-12-14
                            相关资源
                            最近更新 更多