Running JUnit 4 Tests in IntelliJ IDEA 5.1

March 22nd, 2006 by Alexandra Rusina

Note: The tip was originally posted at t800t8.blogspot.com by t800t8



Although the full JUnit 4 support will be added only in IntelliJ IDEA 6.0, you can run JUnit 4 tests right now, in the 5.1 release. Just follow the instructions below.

Note: Of course, you need to add the JUnit 4 JAR file to the classpath first.

1.   Click Settings | File Templates.
2.   In the Templates tab, create new template and name it “JUnit 4 Test Class” or anything you like.
3.   Paste the following code fragment into the text box:

package ${PACKAGE_NAME};

   import org.junit.*;
   import junit.framework.JUnit4TestAdapter;

   #parse("File Header.java")
   public class ${Name} {
       public static junit.framework.Test suite() {
           return new JUnit4TestAdapter(${Name}.class);
       }

       @Before
       public void setUp() {
       }

       @After
       public void tearDown() {
       }

       @Test
       public void testSomething() {
       }
   }


You can see the results on the screenshot.



After you apply the chages, use this template to create JUnit 4 test classes. To run the tests, simply click Run.

Enjoy it and happy unit testing!

Technorati tags:

3 Responses to “Running JUnit 4 Tests in IntelliJ IDEA 5.1”

  1. nils Says:

    I’ve tried this and it works…kind of. The problem I had was that if I run all (working) tests in a package, I get the green bar but each JUnit 4 test class will be colored yellow in the run window. Another problem is that the run window does not show the test method names, but “n test case(s)” instad.

  2. Alexandra Rusina Says:

    Well, it’s a kind of work-around. JUnit4 will be supported in IntelliJ IDEA 6.0 Release. You can try this functionality in the new EAP build (5218) at http://www.intellij.net/eap.

  3. Mitch Says:

    This only works because of the junit 3 syntax that is being used. From what I can tell it\’s completely ignoring the annotations.

Leave a Reply