java 无法在 IntelliJ 中运行 JUnit 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31293987/
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
Unable to run JUnit test in IntelliJ
提问by wizzle
I wanted to start project using TDD. Created the test directory and later changed it to package that is integrated with src direcotry. In both cases I get the same error:
我想使用 TDD 开始项目。创建了 test 目录,然后将其更改为与 src direcotry 集成的包。在这两种情况下,我都会遇到相同的错误:
Class not found: "tests.objectsTest"
找不到类:“tests.objectsTest”
I tried different techniques of importing JUnit jar and none solved problem. Also I tried to rename my test class but it gives no solutons whatsoever.
我尝试了导入 JUnit jar 的不同技术,但没有解决问题。我也尝试重命名我的测试类,但它没有提供任何解决方案。
It seems that IntelliJ or JUnit changes name of the test class. Shouldn't it be "objectsTest.tests"?
IntelliJ 或 JUnit 似乎更改了测试类的名称。不应该是“objectsTest.tests”吗?
I am using JUnit version 4.12 and latest IntelliJ EAP.
我使用的是 JUnit 4.12 版和最新的 IntelliJ EAP。
EDIT This is my project structure:
编辑这是我的项目结构:
Project:
项目:
-.idea
-src
-logic
-objects
-tests
-test
-test.java
src and tests are directories marked as Source and Test. Every package except test is empty. On my other PC with IntelliJ Community Edition everything works fine but on EAP there is this bug. Unfortunatelly I have to use EAP.
src 和tests 是标记为Source 和Test 的目录。除 test 外的每个包都是空的。在我的另一台带有 IntelliJ 社区版的 PC 上一切正常,但在 EAP 上存在此错误。不幸的是,我必须使用 EAP。
test.java code:
test.java 代码:
package test;
import org.junit.Test;
public class test {
@Test
public void canCreateInhabitant(){
}
}
回答by Warkst
Have you checked if you have the JUnit plugin enabled? I (foolishly, shame!) disabled it at some point and was unable to get IDEA to run my tests until I remembered to turn the plugin back on...
您是否检查过是否启用了 JUnit 插件?我(愚蠢,羞耻!)在某个时候禁用了它并且无法让 IDEA 运行我的测试,直到我记得重新打开插件......
回答by Andreas Wederbrand
回答by Dan Pawsey
Sometimes I find that this happens when I try and launch "All tests" from the project folder in the structure view. Launching all tests by right clicking on the test root folder and selecting "all tests" from there seems to solve it.
有时我发现当我尝试从结构视图中的项目文件夹启动“所有测试”时会发生这种情况。通过右键单击测试根文件夹并从那里选择“所有测试”来启动所有测试似乎可以解决它。
回答by markthegrea
Just had this happen to me. When I built via Maven it had a problem. When I fixed the problem, it would run the junits again. Goofy.
刚好发生在我身上。当我通过 Maven 构建时,它遇到了问题。当我解决这个问题时,它会再次运行 junit。傻逼。
回答by Davide Lorenzo MARINO
Check the root directory of your classes. It must be marked as source (for java classes) or test (for java test classes).
检查您的类的根目录。它必须标记为源(对于 java 类)或测试(对于 java 测试类)。
It seems that your directory is not well marked in IntelliJ.
似乎您的目录在 IntelliJ 中没有很好地标记。
回答by tom
Easiest way is:
最简单的方法是:
- Open Class in Intellij and press Ctrl+Shift+T
- Select "Create New Test"
- Now, a new pop up will be opened where you can select Unit Test Library (For your case its Junit4)
- Select the methods which you want to include in test
- And there you go !
- 在 Intellij 中打开 Class 并按 Ctrl+Shift+T
- 选择“创建新测试”
- 现在,将打开一个新的弹出窗口,您可以在其中选择单元测试库(对于您的情况,它是 Junit4)
- 选择要包含在测试中的方法
- 你去吧!