cornercorner
FeaturesPluginsDocs & SupportCommunityPartners

NBUnitTestProperties

This page explains how to setup and use test properties to load libraries required by a Unit test.

Introduction

When a unit test requires some external properties, such as Database or Application Server properties, the XTest test infrastructure can be used to supply property values to a JUnit test. Below are instructions for including external libraries in a unit test. This example will show how to include the Java DB driver in the test.

For an example implementation, checkout visualweb/dataprovider/runtime module and take a look at the files in the test and and test/unit folders

Property file format

There are a couple of ways to create a property file. One is to use project.xml, See Xzajo's blog entry The other is to use a .properties name/value text file.

For either format, if multiple users are going to use this file, then instead of integrating into the repository the properties file with property values, it's better to integrate a template file. Then users can make a copy of the template then just remove the .template from the file name. Don't integrate the properties file with values as others may integrate the file and then it won't work for others.

#*****************************************************************************#
# Each of the character "space" ("blank") used into a property name, must be  #
# changed to a couple of characters: "inverse slash" + "space".               #
# For example: "type.Sun Java System Application Server" must be changed to   #
# "host.Sun\ Java\ System\ Application\ Server".                              #
#*****************************************************************************#
database.driver.name = Derby
database.driver.path = c:/Program\ Files/glassfish-v2/javadb/lib
database.driver.file = derby.jar 

Setting up the XTest configuration files


Loading property files and accessing properties

  1. By default, make sure to load xtest-unit.xml
  2. Create an xtest property and set to an absolute or relative path the properties file
  • <property name="xtest.file_database.properties" location="../propertyfilelocation"/>
  1. Load the properties of the property file
  • <loadproperties srcfile="${xtest_property}"/>
  1. Set the path to the the property value. For example if the property value is a library then set the value to the full path
  • <path id="derby_jar_path">
    
       <pathelement location="${database.driver.path}/${database.driver.file}"/>
</path>
  1. If there are multiple libraries to load create a new path block that includes all libraries to include in the test
  • <path id="required_jars">
    
       <path refid="derby_jar_path"/>   
       <path refid="postgres_jar_path"/>  
</path>
  1. To include libraries at compile-time create a unit-compiler block that references the required jars path block
  2. And to include libraries at runtime create a run-jvm-test block that references the required jars path block (I used the jvm executor for this example)
  3. See the example below for build-unit.xml

Test configuration

  1. To create the test configuration file use an executor, such as jvm for unit testing. Also add a reference to the run-unit-test executor in build-unit.xml.
  • <compiler name="default-compiler" antfile="build-unit.xml" target="unit-compiler" default="true"/>
    
   <executor name="unit-executor" antfile="build-unit.xml" target="run-unit-test"/>
  1. See the example below for cfg-unit.xml

Create a build.xml

  • see example below

Test execution

  1. To run the test, include some xtest options
export BASE=g:/home/tr61
ant -Dnetbeans.dest.dir="$BASE/nbbuild/netbeans" -Dxtest.file_database.properties=$BASE/visualweb/test/data/DefaultDatabase.properties -Dxtest.testtyp
e="unit" -Dxtest.testattribs="stable" -Dnetbeans.javacore.noscan=true

Example

sample test/build-unit.xml

<project name="org.netbeans.modules.visualweb.dataprovider/test-unit" basedir="." default="all">
    <import file="../../../../nbbuild/templates/xtest-unit.xml"/>    

    <loadproperties srcfile="${xtest.file_database.properties}" />
    
    <path id="derby_jar_path">
        <pathelement location="${database.driver.path}/${database.driver.file}"/>
    </path>

    <path id="required_jars">
        <path refid="derby_jar_path"/>   
    </path>
    
   <!-- ========= -->
   <!-- Compilers -->
   <!-- ========= -->
   <target name="unit-compiler" depends="init">
       <buildTests srcDir="unit/src" compileExcludes="**/data/**">
           <classpath>
               <path refid="required_jars"/>
           </classpath>
       </buildTests>
   </target>


   <!-- ========= -->
   <!-- Executors -->
   <!-- ========= -->

   <!-- This target executes tests inside IDE. It is defined in default-build-qa-functional.xml.
        If you need to customize it, you can uncomment the following and override it.-->
   <target name="run-unit-test" depends="init,unit-compiler">
       <executeTests pluginName="jvm">
           <classpath>
               <path refid="required_jars"/>
           </classpath>
       </executeTests>
   </target>
</project>

sample cfg-unit.xml

<mconfig name="cachedrowsetdataprovider">

    <testbag testattribs="stable" executor="unit-executor" name="visualweb/dataprovider/runtime">
        <testset dir="unit/src">
            <patternset>
                <include name="**/*Test.class"/>
                <include name="**/CachedRowSetDataProviderTest.class"/>
            </patternset>
        </testset>
    </testbag>
    <compiler name="default-compiler" antfile="build-unit.xml" target="unit-compiler" default="true"/>
    <executor name="unit-executor" antfile="build-unit.xml" target="run-unit-test"/>
</mconfig>

sample build.xml

 
<project name="visualweb/dataprovider/runtime/test" basedir="." default="all">

    <!-- Name of tested module -->
    <property name="xtest.module" value="visualweb/dataprovider/runtime"/>

    <import file="../../../../nbbuild/templates/xtest.xml"/>
	
    <!-- default testtypes, attributes used when no value is supplied from command line -->
    <property name="xtest.testtype" value="unit"/>
    <property name="xtest.attribs" value="stable,validation"/>
</project>