21.1. Fixture CalculateDiscountConsider the discount tests from Section 3.1 on p. 13, shown again in Figure 21.1. The first row of the table names CalculateDiscount, the fixture that will be used to interpret the tests in the tables. This fixture is the name of a Java classor equivalent in other languages. The second, header, row of the table labels the given column amount and the calculated column discount().
The fixture code for this, in Java, is the class CalculateDiscount, shown in Listing 21.1. This class inherits its testing behavior from fit.ColumnFixture, its superclass. Corresponding to the given column, labeled amount in the table, is the public instance variable amount in the class (line 2 in Listing 21.1). Corresponding to the calculated column discount() is the public method discount() (line 5). Listing 21.1. CalculateDiscount.java1 public class CalculateDiscount extends fit.ColumnFixture { 2 public double amount; 3 private Discount application = new Discount(); 4 5 public double discount() { 6 return application.getDiscount(amount); 7 } 8 } Note Fit fixtures don't follow the usual object-oriented convention of hiding instance variables, because the goal of fixtures is to mediate between the Fit tables and the system under test. The fixtures are not part of the application and are written to expose rather than to hide. The instance variable amount is of the Java type double, a double-precision floating-point number, so the values in the first column of the test rows need to be doubles. As the method discount() returns a double value, the expected values in the second column also need to be doubles. This fixture method calls the method discount() of the system under testof the class Discount, which we don't show. When it runs this table, Fit creates a fixture object of the class CalculateDiscount, as shown in Figure 21.2. Fit passes control to that object by calling its method doTable(). Figure 21.2. Fit Runs the Table
The doTable() code in fit.ColumnFixture, as inherited by CalculateDiscount, reads the labels in the header row of the table to determine the order of the given and calculated columns: amount and discount(). For the third row, the first test, the code in fit.ColumnFixture carries out a sequence of steps. It
Code in fit.ColumnFixture processes each subsequent row of the table in the same way. With the fourth test row, row 6 of Figure 21.1, the actual and expected values don't match. So the cell is colored red, and the actual result, 50.0, as returned from the method call, is added to the cell in the report. When the rows of the table have all been completed, Fit produces the report as HTML, showing the results. Note The class CalculateDiscount simply defines the appropriate instance variables and methods, corresponding to the given and calculated columns. Its superclass, fit.ColumnFixture, does all the work. Although the amount and discount are money, we have used type double in order to keep this first example simple. In Chapter 25, we show how to use a class Money for this. Fit and fit.ColumnFixture use Java reflection to create an object of the fixture class, as well as to change instance variables and call methods of the fixture object. See class Class in Java for further details, if you're interested. Questions & Answers
|