<asserts><assert id="myId" type="my.class.Type" hint="myParseHint" action="MYACTION">...</assert><asserts>
For implementing a solid support of assertion features the JUnit-Addons library from Sourceforge is added as utility archive under the terms of the Apache Software License. The possibilities of defining asserts is mapped directly on the assert methods as defined inside of JUnit and JUnit-Addons. The following actions are implemented for now:
| Name | Description |
|---|---|
| isEqual | Maps to JUnit Assert.assertEquals(). |
| isNotEqual | Maps to JUnit-Addons Assert.assertNotEquals(). |
| isSame | Maps to JUnit Assert.assertSame(). |
| isNotSame | Maps to JUnit-Addons Assert.assertNotSame(). |
| isNull | Maps to JUnit Assert.assertNull(). |
| isNotNull | Maps to JUnit-Addons Assert.assertNotNull(). |
| isTrue | Maps to JUnit Assert.assertTrue(). |
| isFalse | Maps to JUnit-Addons Assert.assertFalse(). |
| isContainedIn | Maps to JUnit-Addons Assert.assertTrue(((Collection)obj).contains(actual)). |
| isNotContainedIn | Maps to JUnit-Addons Assert.assertFalse(((Collection)obj).contains(actual)). |
used for java.lang.Compareable |
|
| isLT | actual is smaller as expected. |
| isNotLT | actual is not smaller (greater or equal) as expected. |
| isGT | actual is greater as expected. |
| isNotGT | actual is not greater (smaller or equal) as expected. |
| isInRange | actual is in specified range according to Comparable rules. |
| isNotInRange | actual is not in specified range according to Comparable rules. |
The possibility of defining asserts inside of xml gives two advantages
over directly using the obj tag and plain %junit; asserts inside
the code.
<objs> can be
used for defining expected objects inside of <assert>.
Just remember the importance of overwriting the equals() and
hashCode() methods of the objects to make them even more valuable
to use under JUnit / DDTUnit.<assert id="complete" action="isEqual" type="junitx.ddtunit.resources.SimpleVO"> <doubleValue>12.4</doubleValue> <integerValue>4711</integerValue> <stringValue>My Text</stringValue> </assert>
assert@idcomplete
and assert@typejunitx.ddtunit.resources.SimpleVO
.
public void testMyService(){
SimpleVO simpleVO = MyServiceUnderTest.getSimpleVO();
addResultToAssert("complete", simpleVO);
}
simpleVO must implement the
methods equals() and hashCode() to meet a clear
expectation of an equality comparison of this type. The same
holds true for the use of JUnit as well.
To be complete: There are three methods defined under DDTTestCase
As the class DDTTestCase is a direct derivation of JUnit
TestCase it is possible to use all assert methods of
junit.framework.Assert directly in the code. It is also possible
to use your own assertion extensions in the testmethod.
Because the provided testdata change, so do the required assertions.
As a rule of thump use this kind of assertions every time you have correlation
with parameter data of xml resources. This provides information about the
contract or specification you want to test.
What are JUnit assert methods good for?
Every time you want to check pre or post conditions of a data centric check use
JUnit assert methods. These should be used similar to the Java assert
extension. For example if you want to check the result of a customer list
returned by a business function.
assertNull(...)
.
The assert action isContainedIn is used
to validate if an object is in a specified set of objects. To define such an assert
you must specify a Collection containing expected values. For example:
<assert id="testResult" type="java.util.Vector" hint="collection" valuetype="boolean" action="isContainedIn"> <item>true</item> <item>false</item> </assert>
The assert action isInRange is used on
java.lang.Comparable datatypes to validate if an object is in a
specified range. To define such an assert you proceed as in the following
example.
<assert id="result" type="range" action="isInRange">
<startIncluded>false</startIncluded>
<start type="long">4711</start>
<endIncluded>false</endIncluded>
<end type="long">4759</end>
</assert></asserts>
start@type and end@type. This
behavior will be replaced by assert@valuetype in future.<startIncluded> or <endIncluded>.