Defining Objects By Constructor and Method Call

<objs><obj id="myId" type="my.class.Type" hint="call" calltype="my.call.Type" >...</obj><objs>

To define objects according to implemented specifications you must be able to call the constructor of this object. Or even call a static method of a factory class. This is just the way you do it in real application code.
Here is an example:

...
  <test id="mySecondTestCase">
   <objs>
    <obj id="myObj" type="junitx.ddtunit.resources.SimpleVO" hint="call">
      <item type="string">My Text</item>
      <item type="int">4711</item>
      <item type="double">12.4</item>
    </obj>
   </objs>
  </test>
...
The definition of object id="myObj" reflects the use of the Java object constructor as defined below
public class SimpleVO {
...
    public SimpleVO(String text, Integer intValue, Double doubValue) {
        // whatever your class should do on instanciation
    }
...
This is equivalent to
<obj id="myObj" type="junitx.ddtunit.resources.SimpleVO" 
  calltype="junitx.ddtunit.resources.SimpleVO" hint="call" method="constructor">
  <item type="string">My Text</item>
  <item type="int">4711</item>
  <item type="double">12.4</item>
</obj>

So you can see that there are three attributes to specify a method/constructor invokation:

  • hint="call" - to specify the use of method/constructor call
  • method="methodName" - specifying the method to call. If you want to use a constructor you can specify method="constructor" or just leave this attribute empty.
  • calltype="my.call.Type" - specifies the class to call the method from. If it is the same as the resulting type specified by type attribute you can leave it empty as well.
If you want to call a method having no arguments write it like this:
<obj id="myObj" type="junitx.ddtunit.resources.SimpleVO" 
  hint="call" method="toString" />

If you specify non static methods the framework will try to instanciate a calltype object by using default constructor.

Actually it is not important to use the <item/> tag name to specify the parameters of the constructor. You can use any name that supports an expressive meaning for the declaration.
To resolve the parameters only the type attribut and the order of the parameters is used to specify the constructor call.
So the example above is equivalet to:

<obj id="myObj" type="junitx.ddtunit.resources.SimpleVO" 
  calltype="junitx.ddtunit.resources.SimpleVO" hint="call" method="constructor">
  <stringValue type="string">My Text</stringValue>
  <integerValue type="int">4711</integerValue>
  <doubleValue type="double">12.4</doubleValue>
</obj>
This declatation reflects the mapping of the constructor parameters on the instance valiables of the class SimpleVO.
sf logo