FeaturesPluginsDocs & SupportCommunityPartners

Notes about Integrating JUnit 4 into NetBeans 5.x

For purposes of testing I've just taken a NetBeans IDE 5.0 and replaced existing junit3.8.jar with the new version. After starting the IDE everything worked without problems. This should not be very surprising as the junit4 contains all the classes of previous version and thus our code remains compatible. These tasks seem to work without problems:

  • running existing tests with public static Test suite()
  • running existing tests without suite method
  • showing the output of the tests in output window
  • showing the special junit output
  • generating new tests and running them

All of this works because of the compatibility and also because NetBeans uses ant's junit task to do all of the execution, so the interface between the IDE and the execution harness remains the same.

Indeed, placing the junit4 as a full replacement of junit3.8 in the IDE classpath prevents the IDE to run on JDK1.4. However an alternative seems to be to keep the junit3.8 in the IDE classpath and use junit4 just as a library for projects. Then the IDE can continue to support 1.4 while project can be developed and run against Java5.

Running JUnit4 in NetBeans 5.x

It seems to be perfectly possible to run tests written against JUnit4 in NetBeans 5.x (with the junit4.jar on test classpath). Of course there is no special support from the IDE, one has to write them by hand. But that is an easy thing to do:

public class PlusTest {
    @Test public void howTheAddGoes() {
        int x = 3;
        int y = 1;
        
        int expResult = 4;
        int result = x + y;
        assertEquals(expResult, result);
    }
}

Which compiles fine. However in order to execute this test in NetBeans 5.x one needs to help the ant's junit task to properly interpret it. To do that just insert following code snippet into the PlusTest:

    public static junit.framework.Test suite() {
        return new JUnit4TestAdapter(PlusTest.class);
    }

Now the tests can be executed by ant and thus by NetBeans as well. The only problem I've noticed is that the test had an empty name and not the "howTheAddGoes" in the JUnit output window. I have not investigated the reason further.

Companion
Projects:
MySQL Database Server   Open JDK: an Open SourceJDK   GlassFish Community: an Open Source Application Server    Mobile & Embedded Community    Open Solaris   java.net - The Source for Java Technology Collaboration   Open ESB - The Open Enterprise Service Bus Powered by