java 寻找有关在 Intellij IDEA 9.x 中使用 JUnit 的教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4183021/
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
Looking for a tutorial on using JUnit with Intellij IDEA 9.x
提问by Robert S. Barnes
I need an absolute beginners guide to using JUnit and Intellij IDEA 9.x together. I'm running JDK 1.6.0_22 on WinXP. I'm looking for answers to the following questions:
我需要一个绝对的初学者指南来一起使用 JUnit 和 Intellij IDEA 9.x。我在 WinXP 上运行 JDK 1.6.0_22。我正在寻找以下问题的答案:
- Do I need to install JUnit or is it already integrated into Intellij? If I need to set it up how?
- Assuming I have the interface and impl for a class I want to test, how do I set up a JUnit project?
- Is there a way to autogen a test skeleton based on the class interface?
- 我需要安装 JUnit 还是它已经集成到 Intellij 中?如果需要怎么设置?
- 假设我有要测试的类的接口和实现,我该如何设置 JUnit 项目?
- 有没有办法根据类接口自动生成测试骨架?
I've got experience with other Unit testing frameworks like, PHPUnit and Boost.Test, so I'm primarily concerned with the mechanics of getting all this set up and running in Intellij.
我有使用其他单元测试框架(如 PHPUnit 和 Boost.Test)的经验,所以我主要关注在 Intellij 中设置和运行所有这些的机制。
Edit
编辑
I'm a complete newb using Intellij.
我是一个使用 Intellij 的新手。
I'm coming from a command-line background, i.e. C++ dev using vim and handwritten make files on Linux.
我来自命令行背景,即在 Linux 上使用 vim 和手写 make 文件的 C++ 开发人员。
I've managed to compile and run some JUnit tests via command line ( downloaded JUnit 4.8.2 and used the -cp swith), but I'm having a heck of a time getting anything set up under Intellij. I tried looking at the online Intellij docs, but haven't found those to be very useful. I looked in Intellij's lib directory and it includes Junit-4.7.jar.
我已经设法通过命令行编译和运行了一些 JUnit 测试(下载了 JUnit 4.8.2 并使用了 -cp 开关),但是我有很长时间在 Intellij 下设置任何东西。我尝试查看在线 Intellij 文档,但没有发现它们非常有用。我查看了 Intellij 的 lib 目录,它包含 Junit-4.7.jar。
I really need some kind of quick start guide with step by step instructions from initial project creation through successfully running the first unit test.
我真的需要某种快速入门指南,其中包含从初始项目创建到成功运行第一个单元测试的分步说明。
采纳答案by Andrzej Doyle
- IDEA comes with JUnit support, but bear in mind that you need to invoke tests on your project - and hence your project will need to have JUnit on its classpath. Hence you don't need to "install" JUnit, but you'll need to make its JAR available to your project as you would with any other third party library (e.g. reference it as a Maven dependency or drop the JAR in
lib/
). - This is where the IDEA support kicks in - I'm 99% sure you don't need to do anything special. Since using JUnit in general simply involves annotating your test methods with
@org.junit.Test
, as per this quick tutorial, IDEA does not require anything further. For any class with this annotation on at least one of its methods, IDEA will give you the option to execute that class as a JUnit test case. (Note: this mayonly be the case for files in a directory that's marked as "Test Sources" in the project structure, but I haven't tested this. It's a good idea to set your project up like this anyway.) - Not by default as part of the JUnit support. There exist other plugins to do this, but in my personal opinion this is not as useful as it might sound. IDEA doeshave integrated code coverage (with the full edition), which (again IMHO) is a better way to ensure that your tests cases cover the full functionality of your class/interface.
- IDEA 附带 JUnit支持,但请记住,您需要对项目调用测试 - 因此您的项目需要在其类路径中包含 JUnit。因此,您不需要“安装”JUnit,但是您需要像使用任何其他第三方库一样将其 JAR 提供给您的项目(例如,将其作为 Maven 依赖项引用或将 JAR 放入 中
lib/
)。 - 这就是 IDEA 支持的用武之地 - 我 99% 确信您不需要做任何特别的事情。由于通常使用 JUnit 只涉及使用 注释您的测试方法
@org.junit.Test
,根据本快速教程,IDEA 不需要任何进一步的内容。对于至少在其方法之一上带有此注释的任何类,IDEA 将提供您将该类作为 JUnit 测试用例执行的选项。(注意:这可能仅适用于在项目结构中标记为“测试源”的目录中的文件,但我尚未对此进行测试。无论如何,这样设置您的项目是个好主意。) - 默认情况下不作为 JUnit 支持的一部分。还有其他插件可以做到这一点,但在我个人看来,这并不像听起来那么有用。IDEA确实具有集成的代码覆盖率(完整版),这(再次恕我直言)是确保您的测试用例覆盖您的类/接口的全部功能的更好方法。
回答by duffymo
For a class named Foo:
对于名为 Foo 的类:
public class Foo
{
public static int add(int x, int y) { return x+y; }
}
write a JUnit test like this:
像这样编写一个 JUnit 测试:
public class FooTest:
{
@Test
public void testAdd()
{
int expected = 5;
int actual = Foo.add(2, 3);
Assert.assertEquals(expected, actual);
}
}
I create /src and /test folders in my IntelliJ project and mirror the package structure under each one.
我在我的 IntelliJ 项目中创建 /src 和 /test 文件夹,并在每个文件夹下镜像包结构。
You can have IntelliJ run all the tests in your project by right clicking on the /test folder and selecting "Run all tests".
您可以通过右键单击 /test 文件夹并选择“运行所有测试”,让 IntelliJ 运行您项目中的所有测试。
If you edit configurations on your "all tests" task, you'll see a checkbox to ask IntelliJ to check code coverage for you. It'll refresh the values after every test run.
如果您在“所有测试”任务上编辑配置,您将看到一个复选框,要求 IntelliJ 为您检查代码覆盖率。它会在每次测试运行后刷新这些值。
回答by vinothkr
I guess you need to download junit library jar and add it to module settings of your project which you can get pressing Shift+Ctrl+Alt+S
In the module settings itself you need to select your test folders. Select the test folders and click on the test sources button on the top To create a test for a corresponding class press Shift+Ctrl+T.
I dont think there are any live templates you may need to create it. Press Ctrl+Alt+S and go to live templates and create one looking into other templates for examples.
我猜您需要下载junit库jar并将其添加到项目的模块设置中,您可以按Shift+Ctrl+Alt+S
在模块设置本身中,您需要选择测试文件夹。选择测试文件夹并单击顶部的测试源按钮 要为相应的类创建测试,请按 Shift+Ctrl+T。
我认为您可能不需要任何实时模板来创建它。按 Ctrl+Alt+S 并转到实时模板并创建一个查看其他模板的示例。