Software QA FYI - SQAFYI

NetBeans Platform Testing Tutorial


NetBeans Platform Testing Tutorial

During development of NetBeans Platform 6.5, an effort was made to improve the testing infrastructure provided for NetBeans Platform applications. Prior to that, many small magical build scripts and other configuration files were needed when setting up the test infrastructure for NetBeans Platform applications. Since then, however, there is inherent support for testing NetBeans Platform application within the NetBeans Platform's build harness scripts. Therefore, unit and functional tests for NetBeans Platform applications are now supported out of the box. This simplification of the testing infrastructure is sometimes referred to as "simpletests", since it greatly simplifies the work necessary to set up such tests for your application.

In this tutorial, you are introduced to setting up the "simpletests" infrastructure and to using the NetBeans Platform's unit testing and functional testing frameworks. These are:
* NB Junit. NetBeans Platform extension to the JUnit testing framework.
* Jelly Tools. NetBeans Platform extension to the Jemmy testing framework.

By the end of this tutorial, you should know how to set up the NetBeans Platform testing infrastructure, how to create unit tests, and how to create functional tests.


Content on this page applies to NetBeans IDE 6.9
* Setting Up the Testing Infrastructure
* Unit Testing on the NetBeans Platform
* Functional Testing on the NetBeans Platform
* Code Coverage on the NetBeans Platform

Setting Up the Testing Infrastructure
When setting up the testing frameworks, you need to enable certain modules that are disabled by default. Then you need to create folders and files in your source structure, where the libraries and tests will live.

If you want to try out these instructions on an actual application prior to trying them out on your own sources, you can use the NetBeans Platform Paint Application, which you can get from the Samples category in the New Project wizard (Ctrl-Shift-N).

1. In the Projects window, right-click your application and choose Properties. In the Project Properties dialog, click "Libraries".
2. Check the "harness" box, adding the entire harness cluster, which provides all the testing modules provided by the NetBeans Platform:
3. Click the Resolve button, which will add modules from the "platform" cluster to your application, as needed by the modules in the "harness" cluster. Notice that there are now no messages about excluded modules and that the Resolve button has disappeared:
Now you must set up a source structure for unit testing and functional testing in your module. In other words, the steps below do not apply to the application's source structure, but to the module that you need to test.
1. Switch to the Files window (Ctrl-2), and expand your module's main node.
2. If it does not exist, create a new folder named "test", within the module's main folder.
4. Within the "test" folder, create a folder named "qa-functional". Underneath "qa-functional", create a folder named "src". You can use the New Folder wizard for these purposes, as follows:
# Within the "test" folder, create a folder named "unit". Underneath "unit", create a folder named "src". #

5. Check that the Files window shows the "test" folder structure as shown below:
6. # Restart the IDE. #
7.In the Projects window notice that there are now two new nodes for your test packages and two new nodes for adding the test libraries to the classpath of the module: #
7. Right-click the "Unit Test Libraries" node and choose "Add Missing Test Dependencies". Then add "JUnit 4" and "NB JUnit". Right-click the "Functional Test Libraries" node and choose "Add Missing Test Dependencies". Then add "JUnit 4", "NB JUnit", "Jemmy", and "Jelly Tools Platform".
If you use "Add Missing Test Dependencies" instead of "Add Unit Test Dependency" and "Add Functional Test Dependency", NB JUnit's recursive dependencies are properly configured. Otherwise INSANE will not be available, which can cause linkage errors when running tests. #
8. Check that the Projects window shows the test library dependencies as shown below:

You have now set up everything needed for creating unit tests and functional tests on the NetBeans Platform. Unit Testing on the NetBeans Platform

The NetBeans Platform's extension to JUnit is named "NB JUnit". It is a separate library with additional support for memory leak tests, deadlock tests, and extended use of logging. More info and motivation can be found at Test Patterns in Java. The basic test class for NB JUnit is NbTestCase.

An example unit test for the NetBeans Platform Paint sample:
import org.netbeans.junit.*;
import org.netbeans.paint.PaintCanvas;

public class PaintCanvasTest extends NbTestCase {
public PaintCanvasTest(java.lang.String testName) {

public void testSetDiam() {
PaintCanvas paintCanvas = new PaintCanvas();
assertEquals("Brush diameter should be set.", 10, paintCanvas.getBrushDiameter());

Create the class above in a package in the Unit Test Packages node in the Projects window. Then right-click the "Paint" project node and choose "Test". If the test is successful, you will see this:

Full article...

Other Resource

... to read more articles, visit

NetBeans Platform Testing Tutorial