How to use JUnit in IDEA 7? Please help a newbie :-)

I'm a newbie to IDEA and I've got a problem in doing a very basic task of creating a JUnit 4 test case. I've checked the help, searched the forum and Internet. There are too many outdated and irrelevant information and I'm not able to get an answer

1. Is it the official way to use the JUnit 4 Synchronizer for creating and running JUnit 4 test case? further questions:
- is there a user guide for "JUnit 4 Synchronizer" plugin?
- Running "Tools > Synchronize JUnit Tests" should create a Testable @annotation, right? Nothing is created after i ran that function. Where should I report the problem?
- Does the JUnit 4 Synchroinzer add JUnit library to the library path? or do I need to manually add the libraries? I suppose I need to do it manually

2. What's the official way to create a test case?
- I've read the following article and it seems I should add a File Template myeslf, right?
http://blogs.jetbrains.com/idea/2006/03/running-junit-4-tests-in-intellij-idea-51/
- The file template in the above links doesn't work on IDEA 7.0.3. Is there an offiical/reference template or should I fix it myself?

3. Coz i haven't created a test case, I can't run it. I guess it should be as straightforward as right-click and run, right? is there anything I need to do?

I suppose many of the above questions/problems I may fix it by myself, but from my experience of using Eclipse, creating and running test cases should be quite a straightforward task by using an IDE, so I think I've missing some steps or did something wrong. Would anybody help?

thank you in advance.

14 comments
Comment actions Permalink

If all you want is a JUnit 4.0 testcase, why not download the libraries, attach them to your project and make a test like:

0
Comment actions Permalink

so it's the recommended way to do everything by myself?

At least, it's better to generate a skeleton by using File Template than writing everything by myself. There is a template already and it's just outdated.

For the lib, I just wonder there might be a better way than manually adding the lib. In Eclipse, it supports using Ctrl-1 Quick Fix (i.e. the equivalent of Intention Action) to add JUnit to path. So it works like, create a new test case (by default it doesn't import the lib; and it has a skeleton already), go to the test case and press Ctrl-1 to import the lib, then write the test code.

In fact, it has a more advanced dialog. If I create a new test on an existing Java class, there is a dialog that allow me to choose the methods that I want to test and it generates a dummy test methods.

I just expect IDEA should be more intelligent than Eclipse in every way! nevermind.

0
Comment actions Permalink

Well, to be honest, when I did an @Test, IntelliJ asked me to import the junit.jar, but I didn't know where IntelliJ retreived the junit lib from. Furthermore, I suspect you'll need more libraries, so I would advise you to either
a) manage them yourself (IntelliJ won't magically know where to retrieve e.g. apache commons from, and neither do Netbeans and Eclipse)
b) use some dependency manager (I use Maven 2, but maybe ivy might be an option too. I've never used it)

So yes, I would recommend to do everything yourself. IntelliJ won't generate the tests for you, and the test template is (from my experience) of limited use. You could go for the unitTest plugin, but the only thing I use, is switchtest, which switches between the test and the implementation.

I agree with the 'intelligence' part: I think that IntelliJ is lacking on this part. I have some time reserved to create a plugin for IntelliJ which will just do what you are describing, but without dialogs. The plan is to create an intention, and when you are at a class file, you press ALT+ENTER, and a 'create testcase' intention will come up, which will create the testcase in the right location. But I haven't start working on it, so if it will ever see the daylight...who knows!

0
Comment actions Permalink

Hello Erik,

EP> I agree with the 'intelligence' part: I think that IntelliJ is
EP> lacking on this part. I have some time reserved to create a plugin
EP> for IntelliJ which will just do what you are describing, but without
EP> dialogs. The plan is to create an intention, and when you are at a
EP> class file, you press ALT+ENTER, and a 'create testcase' intention
EP> will come up, which will create the testcase in the right location.
EP> But I haven't start working on it, so if it will ever see the
EP> daylight...who knows!

Isn't this already covered by the unitTest plugin? If you try to navigate
to the test for a given method and there isn't one yet, the plugin offers
the option of creating the test class (if it doesn't exist) and the corresponding
test, all based on the test location setup. Then the same action is used
for navigation (once the test is in place, that is).

The thing it doesn't do is create tests for all the methods in the class,
but that's something I don't miss (since I'd rather have the tests created
when I'm ready to work on them, not all at once).

Best,
Andrei


0
Comment actions Permalink

Hi Andrei,

Sure it does, but when using that plugin, I have all those fancy icons in my front of my methods, which is something I don't want. Furthermore, the plugin was a bit unstable in my case, but that could have been me, or an older version of the plugin. I just wanted to have an intention for it, also for the fun of learning to develop a plugin, but like I said, the unitTest plugin does almost the same, and I would certainly recommend it (but not for beginners: learn to write your unit test the 'hard way' first, then use a plugin! ;))

0
Comment actions Permalink

Hello Erik,

EP> Sure it does, but when using that plugin, I have all those fancy
EP> icons in my front of my methods, which is something I don't want.

They can be turned off, see the options tab of the plugin settings dialog.
I keep them off as well.

EP> Furthermore, the plugin was a bit unstable in my case, but that
EP> could have been me, or an older version of the plugin.

It's been fairly stable for me, you might want to give the latest version
a try.

EP> to have an intention for it, also for the fun of learning to develop
EP> a plugin,

In that case, by all means, give it a shot, I didn't mean to suggest you
should give away that pleasure. :)

Best,
Andrei


0
Comment actions Permalink

Thanks Erik, intention action to add JUnit 4 jar does work. It's much easier than adding the jar manually.

Thanks everybody. I just want clarify what's the IDEA way of creating test cases and it's clear now. Basically, it's more or less the same as Eclipse by using File Template and intention action to add jar. The only missing thing is the dialog which I don't think is too critical at all.

I do think the Help page of "Creating JUnit Test Cases" is not too helpful as it mentioned the plugins that seem to be broken for IDEA 7.

Attached below is my JUnit file template which is basically trimmed down from the one at http://blogs.jetbrains.com/idea/2006/03/running-junit-4-tests-in-intellij-idea-51/.


>package $;
>import org.junit.*;
>
>public class $ {
>

@Before
public void setUp() {}

>

@After
public void tearDown() {}

>

@Test
public void testSomething() {
}

>}

0
Comment actions Permalink

I have some class which starts like this:
import junit.framework.Assert;
import junit.framework.TestCase;

When I compile it gives me an error saying:

package junit.framework does not exist

I have Junit4.4 and Idea 7, when I run ant from Dos everything works but not in IDEA?!?

any ideas?

0
Comment actions Permalink

You need to add the JUnit JAR file as a dependency to your project: Settings (ctrlalts) | Project Settings | Modules | Select a Module | Dependencies tab. Create a library (Module, project or global) for JUnit and add it.

When you run it from Ant at the DOS prompt, it is working because you (somewhere) have JUnit in the classpath that Ant is using.

0
Comment actions Permalink

Thanks for quick reply Mark, it is working now.

David

0
Comment actions Permalink

ok one last thing:

public Collection toCollection() {
ArrayList c = new ArrayList();
Collection copyList = c;
return copyList;

toCollection() method, c and copyList are geting colored and when I come onto them, they show:
Required tag '@return' is missing more..... (Ctrl+F1)
Local varaible c is redundant more...
Local varaible copyList is redundant more...

Can I turn them off or do I really need them?

Dave

0
Comment actions Permalink

Yes, you can. If you see the light bulb, press Alt + Enter, it is usually the last menu. But look at your code, IDEA recommended you something you should follow.

0
Comment actions Permalink

Well, you can turn them off, but they are there for a reason.

If I look at your code, you definitely need them, because the copyList variable is not a real copy of the list: it's the same list!

So, if you start removing items from the copyList, they will also be removed from list 'c'.

A better way to create a copy of the list is to replace the 'Collection copyList = c;' by 'List copyList = new ArrayList(c)'

Note, however, that any items in the lists are also references, so making changes on the objects in the list (not the list itself) will also result in a changed object in both the lists...

0

Please sign in to leave a comment.