开发者

Looking for a tutorial on using JUnit with Intellij IDEA 9.x [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 9 years ago.

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:

  1. Do I need to install JUnit or is it already integrated into Intellij? If I need to set it up how?
  2. Assuming I have the interface and impl for a class I want to test, how do I set up a JUnit project?
  3. Is there a way to autogen a test skeleton based on the class interface?

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.

Edit

I'm a complete newb using Intellij.

I'm coming from a command-line background, i.e. C++ dev using vim and handwritten make files on Linux.

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 Intelli开发者_运维技巧j's lib directory and it includes 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.


  1. 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/).
  2. 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 may only 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.)
  3. 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 does have 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.


For a class named Foo:

public class Foo
{
   public static int add(int x, int y) { return x+y; }
}

write a JUnit test like this:

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.

You can have IntelliJ run all the tests in your project by right clicking on the /test folder and selecting "Run all tests".

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.


  1. 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

  2. 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.

  3. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜