[RSS]
So far, these are some of the issues I struggled with (gotchas) when setting up Unit tests (under construction) - John Baker

Unit Testing for NetBeans modules

Setup without test infrastructure installed

Setup with test infrastructure installed

After opening a NetBeans module (project) in NetBeans and there is a folder named Functional Test Packages then most likely the test infrastructure has been setup.

If the module doesn't contain Unit Test Packages then, there are some manual steps to follow for the setup. Without this setup, an error will occur when creating a Unit test from a class - "No test target folders found for file "foo.java". Cannot create any tests."

  1. In the Files tab, expand the project, select the test folder and create a new folder named "unit" Then under folder "unit" create a folder named "src" The project refreshes. Switching to the Projects view, Unit Test Packages will be created and now unit tests can be created.
  2. create cfg-unit.xml and build-unit.xml files (see examples below)
  3. Create a unit test: In the Projects window, expand the project node and select a source file under test by right-clicking and choosing Tools->Create JUnit Tests and choose JUnit 4
  4. Restart the IDE to get junit 4 libraries in the classpath


cfg-unit.xml sample

<?xml version="1.0" encoding="UTF-8"?>
<!--
The contents of this file are subject to the terms of the Common Development
and Distribution License (the License). You may not use this file except in
compliance with the License.

You can obtain a copy of the License at http://www.netbeans.org/cddl.html
or http://www.netbeans.org/cddl.txt.

When distributing Covered Code, include this CDDL Header Notice in each file
and include the License file at http://www.netbeans.org/cddl.txt.
If applicable, add the following below the CDDL Header, with the fields
enclosed by brackets [] replaced by your own identifying information:
"Portions Copyrighted [year] [name of copyright owner]"

The Original Software is NetBeans. The Initial Developer of the Original
Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
Microsystems, Inc. All Rights Reserved.
-->
<!-- Configation file to group tests into testbags. For details see
     http://xtest.netbeans.org/xtest_config.html
-->
<mconfig name="visualweb/dataconnectivity">

   <testbag testattribs="stable" executor="unit-executor" name="visualweb/dataconnectivity">
        <testset dir="unit/src">
            <patternset>
                <include name="**/*Test.class"/>
                <exclude name="org/netbeans/modules/visualweb/dataconnectivity/datasource/FooTest.class"/>
            </patternset>
        </testset>
    </testbag>

    <compiler name="default-compiler" antfile="build-unit.xml" target="default-compiler" default="true"/>
    <executor name="unit-executor" antfile="build-unit.xml" target="run-unit-test"/>
</mconfig> 

build-unit.xml sample

<?xml version="1.0"?>
<!--
The contents of this file are subject to the terms of the Common Development
and Distribution License (the License). You may not use this file except in
compliance with the License.

You can obtain a copy of the License at http://www.netbeans.org/cddl.html
or http://www.netbeans.org/cddl.txt.

When distributing Covered Code, include this CDDL Header Notice in each file
and include the License file at http://www.netbeans.org/cddl.txt.
If applicable, add the following below the CDDL Header, with the fields
enclosed by brackets [] replaced by your own identifying information:
"Portions Copyrighted [year] [name of copyright owner]"

The Original Software is NetBeans. The Initial Developer of the Original
Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
Microsystems, Inc. All Rights Reserved.
-->
<project name="org.netbeans.modules.visualweb.dataconnectivity/test-unit" basedir="." default="all">

    <!-- Imports default-compiler, run-unit-test targets referenced from cfg-unit.xml. -->
    <import file="../../nbbuild/templates/xtest-unit.xml"/>
</project> 

  • In build.xml, Add an entry:
<property name="xtest.testtype" value="unit"/>
  • In project.xml, add test dependencies. At minimum, a dependency to org.openide.util and a compile dependency to /test is needed when using Mock objects.Here is an example
         <test-dependencies>
                <test-type>
                    <name>unit</name>
                    <test-dependency>
                        <code-name-base>org.netbeans.modules.projectapi</code-name-base>
                        <recursive/>
                        <compile-dependency/>
                    </test-dependency>
                    <test-dependency>
                        <code-name-base>org.openide.modules</code-name-base>
                    </test-dependency>
                    <test-dependency>
                        <code-name-base>org.netbeans.modules.masterfs</code-name-base>
                    </test-dependency>
                    <test-dependency>
                        <code-name-base>org.openide.util</code-name-base>
                        <compile-dependency/>
                        <test/>
                    </test-dependency>
                </test-type>
                <test-type>
                    <name>qa-functional</name>
                </test-type>
            </test-dependencies>
  1. Go to XTest NetBeans module download and download the NetBeans module for the corresponding NetBeans release you're working on.
  2. Use the Update Center to install the XTest NetBeans module.
  3. Restart NetBeans
  4. For the project under test, select the project, right-click and choose New->Other and select Test Tools and XTest Infrastructure This will generate all the right folders and some default config files.
  5. Create a unit test * In the Projects window, expand the project node and select a source file under test by right-clicking and choosing Tools->Create JUnit Tests

This will create a JUnit test under Unit Test Packages

Using test properties

Resources

For more info: