FeaturesPluginsDocs & SupportCommunityPartners

JUnit 4.0 in NetBeans

Introduction

JUnit 4.0 is a major improvement over existing JUnit 3.8.x.
The main differences are:

  • based on Java 5 annotations
    – test classes and test methods are primarily recognized by Java annotations; annotations are used also for extended declarations of test methods (e.g. to declare that a test method is expected to throw a certain exception)
  • JDK 1.5+ is required (due to usage of annotations)
  • support for timeout for test methods
  • it is possible to declare suite-wide initialization method
  • the new JUnit API is packaged into org.junit.** packages

For more details, see article An early look at JUnit 4.

JUnit is hosted on SourceForge.net. JUnit 4.0 can be downloaded from the JUnit 4.0 download page.

JUnit 4.0 in NetBeans 6.0

Support for JUnit 4 is implemented in NetBeans 6.0, while support for JUnit 3.8 has been maintained. NetBeans 6.0 is bundled with JUnit 3.8.2 and JUnit 4.1. The most used types of projects have both these libraries on their test classpaths so there is no need to add them manually.

The first time the Create JUnit Tests action is invoked on a project, the user is asked about the preferred type of JUnit tests for the project. The appropriate test skeletons are then generated for classes in the project.

Once the user answers the questions about the preferred type of JUnit tests, only one version of JUnit library (the one corresponding to the user's choice) is kept in the project and the other one is removed. If the project does not contain any JUnit library on the test classpath at the moment the action is first invoked, the JUnit library corresponding to the user's choice is added to the test classpath. In either case, just one JUnit library is present in the project once the user created their first test class using the action.

The next time the Create JUnit Tests action is invoked, the preferred type of JUnit library is determined from the version of a JUnit library currently present in the test classpath of the project. If it is JUnit 4.1, test skeletons for JUnit 4 will be generated. If it is JUnit 3.8.2, test skeletons for JUnit 3.8 will be generated. This mechanism implies that if the user manually replaces one JUnit library with the other one in the project's test classpath, they can thus change the preferred type of JUnit test skeletons.

NetBeans 6.0 bundles Ant 1.7.0 which has been accomodated for use with JUnit 4. When JUnit test classes are executed, Ant recognizes whether they are JUnit-3-style or JUnit-4-style tests and correctly handles both of them. One project can even contain a mix of JUnit-3-style and JUnit-4-style test classes and everything should keep working. The JUnit module uses output generated by Ant for displaying test results which means that JUnit test results are displayed correctly both for JUnit-3-style and JUnit-4-style tests.

JUnit 4 tests in NetBeans 5.x

There is no support for generating JUnit-4-style test skeletons in NetBeans 5.x.

Although nothing special has been done for support of running JUnit 4 tests in NetBeans 5.x, it is possible to run them pretty easily – see the report on How to adapt NetBeans 5.x for running JUnit 4 tests.

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