Skip to main content

uPortal Development, Part 4

Posted by gsporar on February 27, 2006 at 9:06 PM PST

Part 3
of this series explained the steps needed to setup a free-form project
like uPortal for debugging. Similar steps are needed in order to be able to use the IDE's
JUnit support. There are two objectives:

  • Be able to compile and run a single JUnit
  • Be able to run the entire suite of JUnit tests

Note: for further information on this topic, refer to
Advanced Free-Form Project Configuration

1. Setup Compilation of a Single File

In order to be able to compile a single file, the IDE needs to know which file has been
selected. As with adding support for the debugger, the IDE provides some help with
setting up the correct targets.

  1. Right-click one of the JUnit test classes in the Projects window and select
    Compile File. The IDE will display this dialog:


  2. Click the Generate button.
  3. The IDE will generate an Ant target, placing it in
    ide-file-targets.xml, which the IDE will open in the editor.
  4. Add a definition for the build.classes.dir property:


    and then save ide-file-targets.xml

  5. Right-click one of the JUnit test classes in the Projects window and select
    Compile File. The file should be be compiled.

2. Running A Single JUnit Test

Running a single JUnit test requires a target that will execute JUnit for
the currently selected file. Copy and paste this target into ide-file-targets.xml:

            <target name="test-selected-file">
                <fail unless="file">Must set property 'file'</fail>
                <junit fork="true" printsummary="on" showoutput="on" dir=".." >
                        <pathelement path="build/WEB-INF/classes"/>
                        <path refid="tests.classpath"/>
                        <path location="build/WEB-INF/classes"/>
                    <formatter usefile="false" type="brief"/>
                    <formatter type="xml"/>
                    <test name="${file}">

And then at the top of the file, add an import of build.xml:

The next step is to modify the project.xml file that is in the
/nbproject folder. Add this entry to it:

                <action name="test.single">

After saving the changes, you should be able to select a single JUnit test in
the Projects window (for example,
and then select Run > Run File > Test (file name) - or
you can just press Ctrl-F6 and see nicely formatted JUnit
output results.

3. Running All JUnit Tests

The uPortal Quickstart's build.xml already has a target
that will run all JUnit tests: runtests. That target will
work fine in the NetBeans IDE, but the output is less than
inspiring: it's just regular text. To see it, right-click the uPortal
entry in the Projects window and then select Test Project.
That output would look better in the JUnit output window
that the IDE provides, but in order for an Ant build target
to use it, the target must meet
these requirements

There are two options for meeting those requirements: modify
the build.xml (probably not a good idea) or create a new custom
Ant target that is only for use by the IDE. The easiest way
to create a custom Ant target is to add it to the ide-targets.xml
file that is in the nbproject folder. Copy and paste this target into ide-targets.xml:

            <target name="run-tests" depends="compiletests" description="Run JUnit Test Suite">
                <junit fork="true" printsummary="on" showoutput="on" dir=".." >
                        <pathelement path="${build.home}/WEB-INF/classes"/>
                        <path refid="tests.classpath"/>
                        <path location="${build.home}/WEB-INF/classes"/>
                    <formatter usefile="false" type="brief"/>
                    <formatter type="xml"/>
                        <fileset dir="${build.home}/WEB-INF/classes">
                            <include name="**/*Test.class"/>
                            <exclude name="**/Abstract*Test.class"/>
                            <exclude name="**/*AbstractTest.class"/>
                            <exclude name="**/DbTest.class"/>

Since this target is dependent on the compiletests target that
is defined in build.xml, the ide-targets.xml file must also be
modified to add this line:

The last step is to modify the project.xml file that is in the
/nbproject folder. It currently specifies that the runtests
target in build.xml should be invoked when Test Project is


Change that entry to specify the new target that is in ide-targets.xml:


After those changes are in place, right-click the entry for
uPortal in the Projects window and choose Test Project
again. The output is much more helpful: