Menu

enum example

rajat
2009-09-11
2012-09-16
  • rajat

    rajat - 2009-09-11

    Is there an example of how enums work in DDTUnit?

    This is what the cookbook says (Defining Constant Objects):

    <test id="mySecondTestCase">

    <objs>

    <obj id="myObj"
    type="junitx.ddtunit.resources.MyEnumerator"

    hint="constant">FIRST_ENUMERATOR</obj>

    </objs>

    </test>

    where the object identified by obj@id="myObj" reflects a Java object
    definition like in the next example

    public enum MyEnumerator {

    FIRST_ENUMERATION, SECOND_ENUMERATION, NULL

    }

    How should the constant myObj be referenced in another object?

    For example, given the following class:

    class Test {

    private MyEnumerator enumerator;

    public void setEnumerator(MyEnumerator enumerator) {

    this.enumerator= enumerator;

    }

    }

    if there is an object of type Test as follows:

    <obj id="myTest" type="Test" hint="call"
    method="setEnumerator">

    <item type="MyEnumerator">myObj</item>

    </obj>

    ... this does not seem to work. I have tried several other combinations,
    including trying to associate via a refid, but none seem to work.

     
  • Joerg Gellien

    Joerg Gellien - 2009-09-13

    Hi Rajat,

    there are different concerns mixed in your request.

    I'll try to claryfy these.

    First of all if you want to instanciate an object there are different options.

    1) instanciate by default constructor and set fields

    a) by reflection => setting fields directly (no setter methods used)

    <obj id="firstObject" type="MyType">

    <firstField type="FirstFieldType">my value</firstField>

    <secondField type= ...

    ...

    </obj>

    b) by setters using Java Bean Standard

    <obj id="secondObj" type="MyType"
    hint="bean">

    <firstField type="FirstFieldType">my value</obj>

    ....

    This will first instanciate an object of type MyType and then uses setter
    methods to set therequested fields.

    => definition above is equivalent to

    MyType secondObj = new MyType();

    secondObj.setFirstField("my value");

    ...

    2) instanciate by selected constructor. This will only provide fields that are
    filled during execution of selected constructor.

    <obj id="thirdObj" type="MyType" hint="call"
    method="constructor">

    <item type="FirstFieldType">my value</item>

    ...

    3) call a method from a specified class to instanciate objects.

    This feature was introduced to support static factory classes in the first
    place. This feature was never intended to provide complex method invocation
    calls. This should be done in the test class.

    Hopefully this makes things a little bit clearer.

    Any ideas on fixing and clarifying documentation are highly welcome.

    If you need more assistance just ask.

    Best regards

    Jörg

     
  • Joerg Gellien

    Joerg Gellien - 2009-09-13

    Hi Rajat,

    just updated the tests.

    If you use the link as provided by Dale you will find an example that should
    reflect your request.

    Best regards

    Jörg

     
  • rajat

    rajat - 2009-10-06

    Hi Jorg,

    I had a follow up question on this. (By the way, I was able to get the enum
    example to work - thanks a lot.)

    In the example that you provided, if we had:

    MyType secondObj = new MyType("typeName");

    secondObj.setFirstField(myValue);

    How would the XML for this look like? That is, is it possible to combine the
    constructor and bean methods in XML? I could not find an example

    of this in the cookbook.

    Thanks,

    Rajat

     
  • Joerg Gellien

    Joerg Gellien - 2009-10-19

    Hi Rajat,

    great to hear the example worked.

    Because DDTUnit xml dialect is not a full fledged language it is not possible
    to define your example completly in a DDTunit data spec.

    It is possible to start with a default constructor and fill up fields of an
    object by using it's setters or use a constructor with all required field
    arguments as parameters.

    It is possible to use a static method of a class to provide the content for a
    field, e.g. using a factory call.

    In your example I would check which part requires changes from testcase to
    testcase and would try to initialize this inside of the xml while the static
    part could be instanciated inside of the testmethod.

    Hope this helps.

    More ideas and examples on usage are very welcome.

    Best regards

    Jörg

     

Log in to post a comment.