views:

634

answers:

5

Should be simple but I couldn't figure it out.

When running my unit test inside IntelliJ, I could not find a way to tell IntelliJ-9.0 that it should use JUnit4 instead of JUnit3.

When a test fails, IntelliJ console displays:

MyTests.testConstraints(MyTests.groovy:20) at

...

com.intellij.junit3.JUnit3IdeaTestRunner.doRun(JUnit3IdeaTestRunner.java:108) at com.intellij.junit3.JUnit3IdeaTestRunner.startRunnerWithArgs(JUnit3IdeaTestRunner.java:42) ...

Do you know how to replace JUnit3 by JUnit4 ?

A: 

Put the JUnit 4 JAR in your CLASSPATH and see if IntelliJ picks it up.

The JUnit plug-in appears to run either version 3 or 4.

I'll bet that it has to do with the way you're writing your JUnit tests. Post one to confirm. If you use the JUnit 4 style, I'll bet IntelliJ would run it properly.

duffymo
I am using a Grails application and it is in the grails CLASSPATH ($GRAILS_HOME/lib/junit-4.8.1.jar). Or maybe, you mean IntelliJ CLASSPATH ? If yes, in which folder should I put the JUnit4 JAR ? (running IntelliJ on Windows at d:/install/IntelliJ)
fabien7474
FYI, two JUnit JARS are in <INTELLIJ_HOME>/lib : junit.jar and junit-4.7.jar. Removing junit.jar does not change anything : com.intellij.junit3.JUnit3IdeaTestRunner.doRun is still called !
fabien7474
+1  A: 

I found it!

  1. Go to Run/Debug Configurations
  2. Add new configuration and choose a JUnit
  3. In the configuration tab, add "-junit4" to the Test run parameters input field

And that's done !

fabien7474
A: 

'com.intellij.junit3' package belongs to IDEA binaries, not to junit3 or junit4. So, the question itself seems to be incorrect in essence - there is no difference in what package name is used by IDEA codebase internally if it correctly executes the tests.

denis.zhdanov
Right and Wrong. com.intellij.junit3' package belongs to IDEA binaries BUT this is the library used for running JUnit test. So it means that if you are using some JUnit 4 features (like @Test annotations), it will not run correctly. Try it yourself. Also, the question is absolutely correct. It is asking about "how to configure IntelliJ for running JUnit 4 tests" and not what is the junit package to use !!
fabien7474
A: 

It sounds like the real problem may be that you are trying to use junit 4 with a grails version less than 1.3. Grails 1.2.x and lower only support Junit 3 tests. Grails 1.3 will finally have junit 4 support. This was discussed on stackoverflow link text

Vinny
It could have been that, but I am using Grails-1.3-RC2.
fabien7474
A: 

You can annotate your test class with an annotation to indicate junit the runner it will use

@RunWith(JUnit4.class) MyTestClass {} 
jaime