Java 使用 IntelliJ IDEA 设置 JUnit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19330832/
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
Setting up JUnit with IntelliJ IDEA
提问by Max
Familiar with Java but unfamiliar with IntelliJ, how does one "get started" with JUnit integration?
熟悉Java但不熟悉IntelliJ,如何“开始”与JUnit集成?
Inspired by Looking for a tutorial on using JUnit with Intellij IDEA 9.xwhich didn't answer my questions and was for an older version of IntelliJ.
灵感来自寻找有关在 Intellij IDEA 9.x 中使用 JUnit 的教程,该教程没有回答我的问题,并且适用于较旧版本的 IntelliJ。
采纳答案by vikingsteve
Basically, you only need junit.jar on the classpath - and here's a quick way to do it:
基本上,您只需要类路径上的 junit.jar - 这是一个快速的方法:
Make sure you have a source folder (e.g.
test
) marked as a Test Root.Create a test, for example like this:
public class MyClassTest { @Test public void testSomething() { } }
Since you haven't configured junit.jar (yet), the
@Test
annotation will be marked as an error (red), hit f2 to navigate to it.Hit alt-enter and choose Add junit.jar to the classpath
确保您有一个源文件夹(例如
test
)标记为Test Root。创建一个测试,例如像这样:
public class MyClassTest { @Test public void testSomething() { } }
由于您还没有配置 junit.jar(还),
@Test
注释将被标记为错误(红色),按 f2 导航到它。按 alt-enter 并选择将 junit.jar 添加到类路径
There, you're done! Right-click on your test and choose Run 'MyClassTest'to run it and see the test results.
到了,你完成了!右键单击您的测试并选择Run 'MyClassTest'来运行它并查看测试结果。
Maven Note: Altervatively, if you're using maven, at step 4 you can instead choose the option Add Maven Dependency..., go to the Search for artifactpane, type junit
and take whichever version (e.g. 4.8 or 4.9).
Maven 注意:或者,如果您使用的是 maven,则在步骤 4 中您可以选择选项Add Maven Dependency...,转到Search for artifact窗格,键入junit
并采用任何版本(例如 4.8 或 4.9)。
回答by Max
- Create and setup a "tests" folder
- In the Project sidebar on the left, right-click your project and do New > Directory. Name it "test" or whatever you like.
- Right-click the folder and choose "Mark Directory As > Test Source Root".
- Adding JUnit library
- Right-click your project and choose "Open Module Settings" or hit F4. (Alternatively, File > Project Structure, Ctrl-Alt-Shift-S is probably the "right" way to do this)
- Go to the "Libraries" group, click the little green plus (look up), and choose "From Maven...".
- Search for "junit" -- you're looking for something like "junit:junit:4.11".
- Check whichever boxes you want (Sources, JavaDocs) then hit OK.
- Keep hitting OK until you're back to the code.
Write your first unit test
- Right-click on your test folder, "New > Java Class", call it whatever, e.g. MyFirstTest.
Write a JUnit test -- here's mine:
import org.junit.Assert; import org.junit.Test; public class MyFirstTest { @Test public void firstTest() { Assert.assertTrue(true); } }
- Run your tests
- Right-click on your test folder and choose "Run 'All Tests'". Presto, testo.
- To run again, you can either hit the green "Play"-style button that appeared in the new section that popped on the bottom of your window, or you can hit the green "Play"-style button in the top bar.
- 创建并设置“tests”文件夹
- 在左侧的项目侧栏中,右键单击您的项目并执行新建 > 目录。将其命名为“测试”或任何您喜欢的名称。
- 右键单击该文件夹并选择“将目录标记为 > 测试源根目录”。
- 添加 JUnit 库
- 右键单击您的项目并选择“打开模块设置”或按 F4。(或者,文件 > 项目结构,Ctrl-Alt-Shift-S 可能是执行此操作的“正确”方法)
- 转到“Libraries”组,单击绿色的小加号(查找),然后选择“From Maven...”。
- 搜索“junit”——您正在寻找类似“junit:junit:4.11”的内容。
- 选中您想要的任何框(Sources、JavaDocs),然后点击 OK。
- 继续点击 OK 直到你回到代码。
编写你的第一个单元测试
- 右键单击您的测试文件夹,“New > Java Class”,任意命名,例如 MyFirstTest。
编写一个 JUnit 测试——这是我的:
import org.junit.Assert; import org.junit.Test; public class MyFirstTest { @Test public void firstTest() { Assert.assertTrue(true); } }
- 运行你的测试
- 右键单击您的测试文件夹并选择“运行‘所有测试’”。普雷斯托,德图。
- 要再次运行,您可以点击出现在窗口底部弹出的新部分中的绿色“播放”样式按钮,或者您可以点击顶部栏中的绿色“播放”样式按钮。
回答by JosiahYoder-deactive except..
I needed to enable the JUnit plugin, after I linked my project with the jar files.
在将项目与 jar 文件链接后,我需要启用 JUnit 插件。
To enable the JUnit plugin, go to File->Settings, type "JUnit" in the search bar, and under "Plugins," check "JUnit.
要启用 JUnit 插件,请转到 File->Settings,在搜索栏中键入“JUnit”,然后在“Plugins”下,选中“JUnit.js”。
vikingsteve's advice abovewill probably get the libraries linked right. Otherwise, open File->Project Structure, go to Libraries, hit the plus, and then browse to
vikingsteve 上面的建议可能会正确链接库。否则,打开文件->项目结构,转到库,点击加号,然后浏览到
C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 14.1.1\lib\
and add these jar files:
并添加这些 jar 文件:
hamcrest-core-1.3.jar
junit-4.11.jar
junit.jar