Configuration

How do I initialize the testdata inside of a DDTTestCase?

To connect the xml resource to your testclass you must implement the abstract method initContext() in your extension of the DDTTestCase class. This method will be executed implicitely during execution preparation.

To facilitate usage there is another method provided my DDTTestCase. Here follows an example how to use the two methods in the default behavior.

...
public void initContext(){
  initTestData("MyClassResource", "MyClassId");
}
...
By providing the two parameters of resource name and classId DDTTestCase will search for a resource of name DDT-MyClassResource.xml in the same ClassPath as the associated testclass . Inside of the resource it will look for the class id provided.

If you provide an absolut resource name starting with / the class will use the resource name unchanged.
...
public void initContext(){
  initTestData("/my/own/path/to/MyClassResource.xml", "MyClassId");
}
        ...
If you like to use the default selection scheme of you can simply write
...
public void initContext(){
  initTestData("MyClassId");
}
        ...
This will be resolved to
public void initContext(){
  initTestData("MyClassId", "MyClassId");
}
where the resource name is is extended to /package/name/of/testclazz/DDT-MyClassId.xml.

Search will be done first on ClassPath then on filesystem.

[top]


How can I extend shortcut notations for TypeAbbreviator?

You will find a properties file junitx.ddtunit.data.processing.TypeAbbreviator.properties. This file must be placed in the same package in the classpath as TypeAbbreviator.class. To modify it just place a copy into the classpath before searching in the jar. Now you can just add the types you need. - Or send a mail and if it is a class provided along with it will be added to the internal properties file.

[top]

All about XML Resources

How can I spare time writing xml data types?

Because it is rather tedious to write something like java.lang.String over and over again a special factory was introdused to the package junitx.ddtunit.data.processing The class TypeAbbreviator contains a dictionary to translate shortcut notatopns used for XML attribute type to the correct type notation. The defined details can be displayed to System.out by just executing this class as application.

[top]


How should I specify my xml schema definition and where to place it?

Every xml resource should start with

<?xml version="1.0" ?>
<ddtunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="http://ddtunit.sourceforge.net/ddtunit.xsd">
  <cluster id="ThisIdShouldBeAdapted">
...
There is a good reason not to use jet another local URL to store the xml schema definition.

The provided URL is hard coded in the EntityResolver of the parser and resolves to a resource that resides in the ddtunit.jar archive. By doing this
  1. you can be sure that you will always use a xml schema that is valid for the active parser
  2. and you do not have the need for online connection or the allowance to access the web if you are behind a corporate firewall for example.

[top]

All about Testing Strategies

How can I test class constructor behavior? - Every time I try to construct my class under test in xml resource an exception is thrown during object creation and the processing is stoped.

Thanks for the example code and actual question to Ted Velkoff.

The intention of the framework is to provide an environment to validate asserts on a class or service/system contract.

Your investigation is focused on a class or service/system under test (CUT, SUT). To do asserts on the behavior of this CUT, good or bad, you will feed it with different parameters and check against expected results.

With this intention in mind you expect your parameters to be valid in the sense of constructing these. Otherwise you wouldn't even start execution of the cut => no parameters, no run.

With this basic concept of operation expects all test parameters provided in the xml resource to be constructable. This will be done before actually executing any of the methods in the test class.

Now you have two choices to phrase the good case execution and a third for exceptional behavior of the example test:

  1. public void testConstructor() throws InvalidDateException {
      CompositeDate subject = new CompositeDate((Integer) getObject("day"),
        (Integer) getObject("month"), (Integer) getObject("year"));
      addObjectToAssert("expectedDay", subject.getDay());
      addObjectToAssert("expectedMonth", subject.getMonth());
      addObjectToAssert("expectedYear", subject.getYear());
    }
    
    with xml resource:
    <test id="test20060101">
    <objs>
      <obj id="day" type="int">1</obj>
      <obj id="month" type="int">1</obj>
      <obj id="year" type="int">2006</obj>
    </objs>
    <asserts>
      <assert id="expectedDay" type="int" action="isEqual">1</assert>
      <assert id="expectedMonth" type="int" action="isEqual">1</assert>
      <assert id="expectedYear" type="int" action="isEqual">2006</assert>
    </asserts>
    </test>
    
  2. or (if CompositeDate.equals() is defined appropriate)
    public void testConstructor() throws InvalidDateException {
      CompositeDate subject = new CompositeDate((Integer) getObject("day"),
        (Integer) getObject("month"), (Integer) getObject("year"));
      addObjectToAssert("expectedCompositeDate", subject);
    }
    
    with xml resource
    <test id="test20000229">
    <objs>
      <obj id="day" type="int">29</obj>
      <obj id="month" type="int">2</obj>
      <obj id="year" type="int">2000</obj>
    </objs>
    <asserts>
    <assert id="expectedCompositeDate"
      type="com.foo.model.calendar.CompositeDate" action="isEqual"
      hint="call">
      <day>29</day>
      <month>2</month>
      <year>2000</year>
    </assert>
    </asserts>
    </test>
    
  3. And last but not least the exceptional case:
    public void testConstructorException() throws InvalidDateException {
      CompositeDate subject = new CompositeDate((Integer) getObject("day"),
        (Integer) getObject("month"), (Integer) getObject("year"));
    }
    
    with xml resource:
    <test id="test20060431">
    <objs>
      <obj id="day" type="int">31</obj>
      <obj id="month" type="int">4</obj>
      <obj id="year" type="int">2006</obj>
    </objs>
    <asserts>
      <exception id="expected" type="com.foo.model.calendar.InvalidDateException"
        action="isInstanceOf" />
    </asserts>
    </test>
    So there is no try/catch block neccessary for expected exception behavior. If an expected exception is not caught during execution of test method an appropriate error is thrown (second example for method testConstructorException).

    Under the links you will find the Java source and the xml resource.

[top]

Problems

What can I do to make this and that to work?
I can fix it right away, if you go to the Issue Tracking page add a new bug/feature request/support request, and most important, ADD A CODE EXAMPLE! If you do that, i will do my best to sort the problem out as quick as possible.

[top]