DPML
Unit Test Automation
HomeUtilitiesStationMetro
Unit Testing

In order to create a test-case we need to declare test scopped dependencies on Ant's Junit runner. A simple approach is shown in the following index.xml file.

index.xml

In the following update to index.xml we have declared three jar files under the test scope for our sample project. The ant-junit file is dependent on both ant and junit so we need to declare the dependencies as well (or risk a NoClassDefFoundException).

<?xml version="1.0" encoding="ISO-8859-1"?>
<index xmlns="dpml:library">

  <project name="demo" basedir=".">
  
    <properties>
      <property name="location" value="New York"/>
      <property name="message" value="Hello from ${user.name} in ${location}"/>
    </properties>
    
    <types>
      <type id="jar" alias="true"/>
    </types>
    
    <dependencies>
      <test>
        <include uri="artifact:jar:org/apache/ant/ant-junit#1.7.0"/>
        <include uri="artifact:jar:org/apache/ant/ant#1.7.0"/>
        <include uri="artifact:jar:junit/junit#4.2"/>
      </test>
    </dependencies>
    
    <filters>
      <filter token="MESSAGE" value="${message}"/>
    </filters>

  </project>

</index>
Executing the build ...

Assuming that testcase sources are placed under the src/test directory the Depot build task will automatically trigger compilation and testcase execution.

$ cd tutorials\tooling\testing
$ build

-------------------------------------------------------------------------
demo#SNAPSHOT
-------------------------------------------------------------------------

init:

prepare:
[x:prepare] Created dir: D:\dpml\tutorials\tooling\testing\target
[x:prepare] Created dir: D:\dpml\tutorials\tooling\testing\target\build\main
[x:prepare] Copying 1 file to D:\dpml\tutorials\tooling\testing\target\build\main
[x:prepare] Created dir: D:\dpml\tutorials\tooling\testing\target\build\test
[x:prepare] Copying 1 file to D:\dpml\tutorials\tooling\testing\target\build\test
[x:prepare] Created dir: D:\dpml\tutorials\tooling\testing\target\test

build:
    [javac] Created dir: D:\dpml\tutorials\tooling\testing\target\classes\main
    [javac] Compiling 1 source file to D:\dpml\tutorials\tooling\testing\target\classes\main
    [javac] Created dir: D:\dpml\tutorials\tooling\testing\target\classes\test
    [javac] Compiling 1 source file to D:\dpml\tutorials\tooling\testing\target\classes\test

package:
      [jar] Created dir: D:\dpml\tutorials\tooling\testing\target\deliverables\jars
      [jar] Building jar: D:\dpml\tutorials\tooling\testing\target\deliverables\jars\demo-SNAPSHOT.jar
      [jar] Creating md5 checksum

test:
    [junit] Created dir: D:\dpml\tutorials\tooling\testing\target\reports\test
    [junit] Executing forked test.
    [junit] Running org.acme.test.DemoTestCase
    [junit] 22/07/2006 16:24:14 org.acme.test.DemoTestCase testTheClock
    [junit] INFO: 4:24 PM, CST
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.141 sec

install:
[x:install] Copying 2 files to D:\system\dpml\data\cache

BUILD SUCCESSFUL
Total time: 4 seconds
Summary

This example has demonstrated the relatively easy process of adding testcases to a project. In addition we have demonstrated one mechanism concerning the declaration of dependencies - specifically the usage of the uri attribute on dependency include statements. However this approach is error-prone as it assumes that the dependencies listed fully complement each other. For example in the dependencies listed above we have not included ant-lancher, ant-trax or ant-xslp. Futhermore this approach presumes that we have the version combination correct.

An alternative (and more reliable) approach is presented in the next tutorial where we import an ant module and simply reference the ant-junit resource as a single dependency entry.