Java 在 IntelliJ 中创建测试类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4346786/
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
Create Test Class in IntelliJ
提问by Scruffers
I'm working within the traditional Maven Java project structure inside IntelliJ, e.g.
我在 IntelliJ 中的传统 Maven Java 项目结构中工作,例如
main/java/com/x/y
test/java/com/x/y
When I create a class called Foo in main/java/com/x/yusing IntelliJ I would like to automatically have a file called FooTest.java created in test/java/com/x/y.
当我使用 IntelliJ在main/java/com/x/y 中创建一个名为 Foo 的类时,我想自动在test/java/com/x/y 中创建一个名为 FooTest.java 的文件。
In Eclipse there is a button on the toolbar that will do much of the work involved in creating a skeleton test class, does anyone know of something similar in IntelliJ?
在 Eclipse 中,工具栏上有一个按钮可以完成创建骨架测试类的大部分工作,有谁知道 IntelliJ 中的类似内容?
Thanks,
谢谢,
采纳答案by Jarl
Use the menu selection Navigate-> Test, or Ctrl+Shift+T(Shift+?+Ton Mac). This will go to the existing test class, or offer to generate it for you through a little wizard.
使用菜单选项Navigate-> Test或Ctrl+ Shift+ T(Mac 上的Shift+ ?+ T)。这将转到现有的测试类,或者通过一个小向导为您生成它。
回答by rmoestl
Alternatively you could also position the cursor onto the class name and press alt+enter(Show intention actions and quick fixes). It will suggest to Create Test.
或者,您也可以将光标定位到类名上,然后按alt+ enter(显示意图操作和快速修复)。它会建议创建测试。
At least works in IDEA version 12.
至少适用于 IDEA 12 版。
回答by user1412523
*IntelliJ 13 *(its paid for) We found you have to have the cursor in the actual class before ctrl+Shift+Tworked.
*的IntelliJ 13 *(其支付),我们发现,你必须有实际的类光标之前ctrl+ Shift+T的工作。
Which seems a bit restrictive if its the only way to generate a test class. Although in retrospect it would force developers to create a test class when they write a functional class.
如果它是生成测试类的唯一方法,这似乎有点限制。尽管回想起来,这会迫使开发人员在编写功能类时创建测试类。
回答by Hamish
I can see some people have asked, so on OSX you can still go to navigate->test or use cmd+shift+T
我可以看到有人问过,所以在 OSX 上你仍然可以去导航->测试或使用cmd+ shift+T
Remember you have to be focused in the class for this to work
请记住,您必须全神贯注于课堂上才能发挥作用
回答by Da_Vicki
Use @Test annotation on one of the test methods or annotate your test class with @RunWith(JMockit.class) if using jmock. Intellij should identify that as test class & enable navigation. Also make sure junit plugin is enabled.
在测试方法之一上使用 @Test 注释,或者如果使用 jmock,则使用 @RunWith(JMockit.class) 注释您的测试类。Intellij 应该将其标识为测试类并启用导航。还要确保启用了 junit 插件。
回答by Hymany
I think you can always try the Ctrl+ Shift+ Ato find the action/command you need.
Here you can try to press Ctrl+ Shift+ Aand input ?test? to find the command.
我想,你还可以尝试使用Ctrl+ Shift+A找到动作/命令你的需要。
在这里您可以尝试按Ctrl+ Shift+A并输入 ?test? 找到命令。
回答by Adriaan Koster
With the cursor on the class name declaration I do ALT + Return and my Intellij 14.1.4 offers me a popup with the option to 'Create Test'.
将光标放在类名声明上,我执行 ALT + Return,我的 Intellij 14.1.4 为我提供了一个带有“创建测试”选项的弹出窗口。
回答by Abraham Hernandez
回答by Jugal Panchal
- Right click on project then select new->directory. Create a new directory and name it "test".
- Right click on "test" folder then select Mark Directory As->Test Sources Root
- Click on Navigate->Test->Create New Test
Select Testing library(JUnit4 or any)
Specify Class Name
Select Member
- 右键单击项目,然后选择新建->目录。创建一个新目录并将其命名为“test”。
- 右键单击“test”文件夹,然后选择将目录标记为->测试源根
- 点击导航->测试->创建新测试
选择测试库(JUnit4或任何)
指定类名
选择成员
That's it. We can modify the directory structure as per our need. Good luck!
就是这样。我们可以根据需要修改目录结构。祝你好运!