Catching Expected Exceptions

<asserts><exception id="myId" type="my.exception.Type" hint="myParseHint" action="MYACTION">...</exception><asserts>

In fact a lot of testing goes into checking if certain exceptions are raised as expected error conditions.
To support this feature in JUnit you would use a code snipplet like this

  public void testExpectedException(){
    try{
      instanceUnderTest.methodUnderTest(myParam);
      fail("An exception was expected");
    } catch (MyExpectedException e){
      // whatever special checks you want to add
      // if pass, all is fine
    }
  }
In DDTUnit this task would look as following:
...
  public void testExpectedException(){
      my.param.Clazz myParam = (my.param.Clazz) getObj("myParam");
      instanceUnderTest.methodUnderTest(myParam);
  }
... 
== with the xml definition:
  <test id="testExpectedException">
   <objs>
     <obj id="myParam" type="my.param.Clazz">Content</obj>
   </objs>
   <asserts>
     <exception id="expectedException" type="my.expected.Exception"
        action="isEqual">Message of Exception</exception>
   </asserts>
  </test>
If no exception is raised during method execution but one was defined, a junit.framework.AssertFailedError is thrown.
Exception definition classes must be derived from type java.lang.Throwable.
Possible actions are
Valid Assert Actions
NameDescription
isEqualTwo exceptions are equal if they are of the same type and they contain the same method as defined by getMessage()
isSimilarTwo exceptions are similar if they are of the same type and the message of the defined exception is contained in the one of the actual exception.
isInstanceOfOne exception object is of the same instance as a specified class type if it has the same type or a derived type as the specified class.
Because expected exceptions are a special form of assertions they are placed near the assert definitions under <asserts>.

Here you can see an example taken from the test suite.

sf logo