Java 黄瓜无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51151585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cucumber cannot be resolved to a type
提问by Kakashi - Sensei
I created a RunTest class to run my test scenarios using Cucumber with JUnit. To run the tests before, I need to import into my project the RunWith class (@RunWith) and pass as parameter the Cucumber.class. Then, the RunWith parameter of the class recognizes more of the parameter that is passed to it, no. The eclipse displays the message:
我创建了一个 RunTest 类来使用 Cucumber 和 JUnit 来运行我的测试场景。要运行之前的测试,我需要将 RunWith 类 (@RunWith) 导入到我的项目中,并将 Cucumber.class 作为参数传递。然后,类的 RunWith 参数识别更多传递给它的参数,不。eclipse 显示消息:
Multiple markers at this line - Class can not be resolved to a type. - Cucumber can not be resolved to a type. - The annotation @RunWith must define the attribute value
此行有多个标记 - 无法将类解析为类型。- Cucumber 无法解析为类型。- 注解@RunWith 必须定义属性值
I'm using Maven to organize my JARS files. Following is the code and error screens.
我正在使用 Maven 来组织我的 JARS 文件。以下是代码和错误屏幕。
My POM.XML
我的 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>
回答by nikhil Ingole
Import the cucumber.api.junit.Cucumber
class. It seems you are using Eclipse IDE, so you can import the classes using the Ctrl+Shift+o
(alphabet 'o' not zero) shortcut.
导入cucumber.api.junit.Cucumber
类。您似乎正在使用 Eclipse IDE,因此您可以使用Ctrl+Shift+o
(字母“o”不是零)快捷方式导入类。
(Ctrl+Shift+O
is 'Organise Imports', and will add any missing imports, remove any unused ones, and order all of your imports). The command is also found under Source > Organise Imports.
(Ctrl+Shift+O
是“组织进口”,将添加任何缺失的进口,删除任何未使用的进口,并订购您的所有进口)。该命令也可以在 Source > Organize Imports 下找到。
回答by Rocky
Just do Ctrl+Shift+T and type RunWith check if the jar is correctly imported into your work space after maven dependencies are resolved.
只需执行 Ctrl+Shift+T 并键入 RunWith 检查在解决 Maven 依赖项后 jar 是否正确导入到您的工作空间。
回答by Kakashi - Sensei
I removed my POM.xml file from the dependency below:
我从下面的依赖项中删除了我的 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>
And I added the 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>
Done that I was able to do import.
完成,我能够进行导入。
回答by Amanda Fernandes
I ran into the same problem. I am using a Maven project in Eclipse. My problem seemed to be the JRE version I was using. My Maven projects all had an old J2SE-1.5 I switched to 1.8 within Eclipse and the issue went away. Hope this helps!
我遇到了同样的问题。我在 Eclipse 中使用 Maven 项目。我的问题似乎是我使用的 JRE 版本。我的 Maven 项目都有一个旧的 J2SE-1.5,我在 Eclipse 中切换到 1.8,问题就消失了。希望这可以帮助!
回答by Vineesh
As mentioned above, adding the below dependency resolved my issue:
如上所述,添加以下依赖项解决了我的问题:
<!-- 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>
回答by user11620140
Step 1: Mouse hovering on error shows, class cucumber cannot be resolved to a type. so, write the import statement manually and the error will be gone.
步骤一:鼠标悬停在错误提示上,类黄瓜无法解析为类型。所以,手动编写导入语句,错误就会消失。
import cucumber.api.junit.Cucumber;
进口黄瓜.api.junit.Cucumber;
Step 2: Remove scope from pom.xml file for cucumber-JUnit dependency.
第 2 步:从 pom.xml 文件中删除 Cucumber-JUnit 依赖项的作用域。
回答by santosh venkataswamy
Downloaded all the latest jar maven dependencies from "mvnrepository". This worked for me.
从“mvnrepository”下载了所有最新的 jar maven 依赖项。这对我有用。
回答by SAPNA THAKUR
Please add this dependency and remove old one. it worked for me.
请添加此依赖项并删除旧的依赖项。它对我有用。
<!-- 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>
回答by Sandunika Wijerathne
I also faced the same issue in IntelliJ idea, When I check in the imported libraries it was like below screenshot.
我在 IntelliJ 想法中也遇到了同样的问题,当我检查导入的库时,它就像下面的截图。
So I remove scope tag from my import in pom file.Then it worked.
所以我从 pom 文件中的导入中删除了范围标记。然后它就起作用了。
Earlier
早些时候
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
Fix
使固定
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
</dependency>
Now it is working fine
现在它工作正常
回答by AlexPes
For me the same issue was resolved by deleting .m2/repository, restart IDEA with dependencies as follows:
对我来说,通过删除 .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>
Hope, it works for you as well.
希望,它也适用于你。