Section 21.1. Fixture CalculateDiscount


21.1. Fixture CalculateDiscount

Consider 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().

Figure 21.1. Fit Table for Testing Discount

CalculateDiscount

amount

discount()

0.00

0.00

100.00

0.00

999.00

0.00

1000.00

0.00

1010.00

50.50

100.00

55.00

1200.00

60.00

2000.00

100.00


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.java
 1 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

  • Takes the text value 0.00 in the first cell and converts it to a double, the type of the instance variable amount.

  • Assigns this double value to the instance variable amount of the fixture object.

  • Calls the method discount() of the fixture object, getting the calculated result, a double. This fixture method calls the system under test, as shown in Figure 21.2.

  • Takes the text value 0.00 from the second cell, the expected result, and converts it into a double, the return type of the method.

  • Compares the actual double that was returned from the fixture method against the expected result. They are equal, so the second cell is colored green.[1]

    [1] See Section 3.2 on p. 14 for details of the coloring scheme.

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

Q1:

What does the () in the header mean?

A1:

The label of a calculated column has a suffix of () to signify that a method will be called. This method may simply calculate a value and return it, or it may carry out some action with side effects.

Q2:

Do the calculated columns have to be after all the given columns?

A2:

No, not necessarily. The order matters, however, as the columns are processed from left to right, regardless.

Q3:

What happens if the value in a cell cannot be treated as a double?

A3:

The cell is marked with yellow and an error message given.

Q4:

What happens if a method to be called in the system under test takes several parameters?

A4:

In that case, more given columns and associated instance variables will be needed. We'll see that in the next example, in Section 21.2.

Q5:

What if the two doubles are not quite the same?

A5:

They won't match. However, you can use fit.ScientificDouble instead; this checks the actual value against the expected value according to the precision of the expected value.

Q6:

It's odd having a public instance variable in the fixture class.

A6:

Yes, we're usually encouraged to hide such implementation details. Here, however, the fixture class is simply acting as a bridge between the Fit table and the system under test, so different rules can apply. We'll see in Chapter 22 that ActionFixture takes a different approach to passing data from the table to the fixture.

Q7:

Can Fit be used for other sorts of testing, such as response time?

A7:

Yes. You could have a given field with the maximum response time allowed, and a boolean calculated field for whether the response was within the time. Or, you could test that some given percentage of the responses were under the given time.



    Fit for Developing Software. Framework for Integrated Tests
    Fit for Developing Software: Framework for Integrated Tests
    ISBN: 0321269349
    EAN: 2147483647
    Year: 2005
    Pages: 331

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net