How to Work with JUnit in the NetBeans IDE

July

In this tutorial, you will convert JUnit to a NetBeans project and know how to build and perform test runs of the JUnit project in the NetBeans IDE.

Introduction

JUnit is a simple, open source framework for writing and running automated tests. It is an instance of the xUnit architecture for unit testing frameworks.
This tutorial shows you how to use the NetBeans IDE for JUnit development.

The NetBeans Integrated Development Environment (IDE) is a free, open-source Integrated Development Environment for software developers. The NetBeans IDE provides developers with all the tools they need to create professional cross-platform desktop, enterprise, web, and mobile applications. In this tutorial, you learn how to convert a JUnit project into a Netbeans project to develop, build, and test JUnit within the Netbeans IDE.

For more information on how to work with the NetBeans IDE, see the Support and Docs page.

Software needed

Before you begin, you need to install the following software on your computer: NetBeans IDE 6.0.
Since NetBeans 6.0 is under development now, use NetBeans IDE 6.0 Preview (M10) available from NetBeans IDE 6.0 Preview (M9) Download, or check the latest working version.

Notations used in this documentation

  • <NETBEANS_HOME> - the NetBeans IDE installation directory
  • <USER_HOME> - the user's home directory. Example: "C:\Documents and Settings\user_home"
  • <junit_src_root> - the directory with JUnit sources.

Exercises

Converting JUnit to a Netbeans Project

To download the JUnit sources:

1. Download the JUnit source code from CVS repository to the <junit_src_root> directory. CVSROOT=:pserver:anonymous@junit.cvs.sourceforge:/cvsroot/junit. You need to download the junit directory.

To create the JUnit NetBeans Project:

1. From the main menu, choose File > New Project.
The New Project wizard opens.

2. In the Categories list, select "Java", in the Projects list, select "Java Project with existing ANT Script," and click Next.

3. At the Name and Location page, specify the locations of the JUnit's project files and the build.xml file. In this case, the JUnit project files are located at C:\tmp\junit\.

Click Next.

4. At the Build and Run Actions page, the IDE sets the following Ant targets in the fields listed below:

  • Build Project - build
  • Clean Project - clean
  • Generate Javadoc - javadoc
  • Run Project - empty
  • Test Project - samples-and-tests

Change the value of Test Project field to dist.

Click Next.

5. At the Source Package Folders page of the wizard, specify <junit_src_root>/src as the source package folder.

6. Click the Includes/Exludes button and specify the following values in the fields listed below:

  • Excludes - **/imposterization/**,**/theories/test/**

Note: This step is required because you do not need certain packages from the JUnit source. Examine build Ant task in the JUnit's build.xml to ensure you should exclude the packages mentioned above.

Click Next.

7. At the Java Sources Classpath page, add the hamcrest-core-1.1.jar file under the <junit_src_root>/lib directory to the Java Sources Classpath list.

Click Finish.

The Projects window contains the project node for the JUnit project you have just created. Now you can develop JUnit within the NetBeans IDE.

Building the JUnit Project

To compile the JUnit project files, do the following:

1. In the Projects window, right-click the JUnit project node and choose Build or Clean and Build from the pop-up menu.

The JUnit source code is compiled and you see the "BUILD SUCCESSFUL" message in the Output window.

Testing the JUnit Project

To perform testing of the JUnit project, do the following:

1. In the Projects window, right-click the JUnit project node and choose Test from the pop-up menu, or press Alt+F6.

A test run of the JUnit project is performed and you see test results in the Output window.

Summary

In this tutorial, you learned how to start JUnit development in the NetBeans IDE. You have completed the following exercises:

  • Convert JUnit into Netbeans Project.
  • Build JUnit in NetBeans IDE.
  • Test JUnit in NetBeans IDE.

See Also

References:

  • NetBeans IDE 6.0 Preview Documentation:
  • Got issues:
  • FAQ List: NetBeansUserFAQ

Attachments

NetBeans