Java Reference
In-Depth Information
The
TestTree
test case checks the functionalities of the classes
Node
and
indirectly
Arc
.
public class
TestTree
extends
TestCase {
public
void testLeaf(){
String label
#
"leaf label";
Node leaf
#
new
Node(label);
assertTrue("Should be a leaf",leaf.isLeaf());
assertEquals(label,leaf.label());
}
public
void testOneLevel(){
Node leftLeaf
#
new
Node("left");
Node rightLeaf
#
new
Node("right");
String toLeft
#
"to the left";
String toRight
#
"to the right";
Node root
#
new
Node("root");
root.addChild(toLeft,leftLeaf);
root.addChild(toRight,rightLeaf);
assertFalse("root shouldn't be a leaf", root.isLeaf());
assertSame(leftLeaf,root.follow(toLeft));
assertSame(rightLeaf,root.follow(toRight));
}
}
Finally the
TestClassifier
test case checks whether the classifier assigns the
category correctly to an item. To test this functionality, four main steps are
required:
■
define the type of item, i.e. its features;
■
build a decision tree;
■
apply it to some items;
■
check if the assigned categories are correct.
Given the classification tree shown in Figure 4.1, we have to define which
items will be used to test the classifier. Since the classification algorithm
follows a path through the decision tree, we will use items that force it to
traverse all the paths in the test tree. There are four possible paths in the
decision tree as described in Figure 4.1.
The
TestClassifier
test case has only one test method and a helper method
that builds the decision tree. The test method applies the classifier to the
four items described in Table 4.1.
Table 4.1
Test items
Item
AC
ABS
Expected category
1
Yes
Yes
High
2
Yes
No
Medium
3
No
Yes
Medium
4
No
No
Low