23.2. Testing Ordered ListsWe now look at the fixture for the discount group test from Section 5.2 on p. 34, as shown again in Figure 23.3. In this case, we make use of order, an extra column in the table.
The fixture class DiscountGroupOrderedList is shown in Listing 23.3. As a subclass of fit.RowFixture, this class defines the methods query() and getTargetClass(). Listing 23.3. DiscountGroupOrderedList.java1 public class DiscountGroupOrderedList extends fit.RowFixture { 2 public Class getTargetClass() { 3 return OrderedDiscountGroup.class; 4 } 5 public Object[] query() throws Exception { 6 DiscountGroup[] groups = DiscountGroup.getElements(); 7 OrderedDiscountGroup[] ordered = 8 new OrderedDiscountGroup[groups.length]; 9 for (int i = 0; i < groups.length; i++) { 10 DiscountGroup g = groups[i]; 11 ordered[i] = new OrderedDiscountGroup(i+1, 12 g.getFutureValue(),g.getMaxOwing(), 13 g.getMinPurchase(),g.getDiscountPercent()); 14 } 15 return ordered; 16 } 17 } Method getTargetClass() of the fixture (line 2 in Listing 23.3) returns the class of the elements of the list, OrderedDiscountGroup. RowFixture uses the returned Class to check that instance variables correspond to each of the header labels in Figure 23.3. The method query() of the fixture (line 5 in Listing 23.3) gets the elements of the actual list by calling a method in DiscountGroup. These elements are then mapped into elements of a temporary array of class OrderedDiscountGroup, with the order added, as shown in Listing 23.4. Figure 23.4 shows some of the objects concerned when Fit runs this table. Inherited by DiscountGroupOrderedList, the doTable() method of RowFixture carries out four major steps:
Before it gathers up the expected elements from the table in Figure 23.3, RowFixture uses the five labels in the header row of the table. Each of these labels corresponds to an instance variable in OrderedDiscountGroup, the class returned by getTargetClass() of the fixture. The first label is order, which corresponds to the public instance variable order in class OrderedDiscountGroup. This instance variable is an int, so RowFixture uses the text in the first cell of each of the expected rows as an int. The other columns correspond to instance variables of type String and double, so some of the element values in these columns need to be converted before the comparisons can be made. To compare the actual and expected lists, RowFixture attempts to match each expected row against an actual row, based on the table labels. The fixture starts with the leftmost column of the table and works across the cells in the row as a match is made. Questions & Answers
|