Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 Intellij'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.

like image 718
Robert S. Barnes Avatar asked Nov 15 '10 09:11

Robert S. Barnes


People also ask

How do I run JUnit in IntelliJ?

In eclipse, you go to test class-> right click -> run-> as junit. Somewhat like this we do. Its similar in intellij too. Go to test class right click and run as junits.

Does IntelliJ have JUnit?

IntelliJ IDEA works with multiple testing frameworks out of the box, for example, JUnit, Spock, TestNG, or Cucumber..

How do I create a JUnit test case in IntelliJ?

Add a new test In your production code in the editor, place the caret at the class for which you want to create a test, press Alt+Enter , and select Create Test. In the Create Test dialog, select the library that you want to use. If you don't have the necessary library yet, you will be prompted to download it.


1 Answers

  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.
like image 82
Andrzej Doyle Avatar answered Nov 01 '22 05:11

Andrzej Doyle