Java 缺少“作为 JUnit 测试运行”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6365920/
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
Missing "Run as JUnit Test"
提问by amaran
I created a JUnit 4 test in Eclipse by right-clicking on a Java class and selecting New JUnit Test Case. When I right-click the test class I get "Run on Server", but not "Run as JUnit Test". I am using Eclipse 3.6.1.
我通过右键单击 Java 类并选择 New JUnit Test Case 在 Eclipse 中创建了一个 JUnit 4 测试。当我右键单击测试类时,我得到“在服务器上运行”,而不是“作为 JUnit 测试运行”。我正在使用 Eclipse 3.6.1。
回答by hoipolloi
I think I see the problem. You need to have an actual test in the file before Eclipse identifies it as a test case. Try inserting the following:
我想我看到了问题。在 Eclipse 将其识别为测试用例之前,您需要在文件中进行实际测试。尝试插入以下内容:
@Test
public void foo() {
}
回答by u290629
Make sure your class has JUnit traits (extends from
TestCase
, or use@Test
etc);Right-click "Run As" -> "Run Conciguration" -> Create JUnit test from left icon "JUnit" anyway;
确保您的类具有 JUnit 特性(从
TestCase
、 或使用@Test
等扩展);右键单击“运行方式”->“运行配置”->无论如何从左侧图标“JUnit”创建JUnit测试;
回答by Jeff Axelrod
In my case, Eclipse must have reached a corrupt state. Restarting Eclipse fixed the problem.
就我而言,Eclipse 必须已达到损坏状态。重新启动 Eclipse 解决了该问题。
回答by Sajan Chandran
The eclipse shortcut to run Junit test is Alt+Shift+X, T
.
If its not working just press Alt+shift+X
a menu will popup just look for Junit.
运行 Junit 测试的 Eclipse 快捷方式是Alt+Shift+X, T
. 如果它不起作用,只需按Alt+shift+X
一个菜单就会弹出,只需查找 Junit。
回答by iharbeck
Make sure to have a valid default constructor for your test class.
确保您的测试类具有有效的默认构造函数。
回答by Tad Lamb Jr
In my case, the problem was different. I was converting a TestNG test to JUnit. The @Test annotation was satisfied by the TestNG import, but that was the wrong annotation. I removed the TestNG import and added the JUnit import for @Test, and the right-click menu option to run as a JUnit test appeared.
就我而言,问题是不同的。我正在将 TestNG 测试转换为 JUnit。TestNG 导入满足 @Test 注释,但这是错误的注释。我删除了TestNG导入并为@Test添加了JUnit导入,并且出现了作为JUnit测试运行的右键菜单选项。
回答by Abdull
I ran into these symptoms when importing an existing project into a Kepler-based Eclipse IDE for Java Developersversion.
在将现有项目导入到基于Kepler 的 Eclipse IDE for Java Developers版本时,我遇到了这些症状。
Importing the project into a Luna-based Eclipse IDE for Java EE Developerscorrectly set set it to a Java project (project icon now includes that little J) and now allows running JUnit tests.
将项目导入Java EE 开发人员的基于 Luna 的 Eclipse IDE正确地将其设置为 Java 项目(项目图标现在包含那个小J),现在允许运行 JUnit 测试。
回答by Thomas Weller
In my case the Java Build path (.classpath
file) was corrupt. In particular it had a merge conflict which was unresolved. Therefore, the JUnit 4 library was missing.
在我的情况下,Java Build 路径(.classpath
文件)已损坏。特别是它有一个未解决的合并冲突。因此,缺少 JUnit 4 库。
回答by Wenyu
I used to have a similar problem and it turned out that was because I forgot "extends Specification" after the "ClassToBeTestedSpec" in the declaration.
我曾经有过类似的问题,结果证明是因为我在声明中的“ClassToBeTestedSpec”之后忘记了“扩展规范”。
回答by William Schultz
Make sure that you include import org.junit.Test;and the tests have @Testbefore them, I have missed that before when coping some functions from other files.
确保包含import org.junit.Test; 并且测试之前有@Test,我之前在处理其他文件中的某些功能时错过了这一点。