RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Apply Fit and FitNesse to Run Web-Based Acceptance Tests  : Page 3

Discover how to create a suite of automated acceptance tests that your entire development team can share and maintain at a very low cost.

Linking Test Tables
The test table shown in Table 1 is useful as a communication tool; however, the real power of Fit comes from the ability to execute these tables as tests against the application itself. To run any table as a test you must write a small amount of code in the form of a Fixture class. Here is the Fixture class that supports Table 1:
package com.devx.fit;

import com.devx.fit.app.BookingMethod;
import com.devx.fit.app.RateCalculator;

import fit.ColumnFixture;

public class RentalRates extends ColumnFixture {
  public int age;
  public String carType;
  public BookingMethod bookingMethod;

  private RateCalculator calculator = new RateCalculator();

  public Boolean rentalAllowed() {
    return calculator.isRentalAllowed(age, carType);

  public double dailyRate() {
    return calculator.calculateRate(age, carType, bookingMethod);

  public Object parse(String s, Class type) throws Exception {
    if (type == BookingMethod.class) {
      return BookingMethod.valueOf(s.toUpperCase());
    return super.parse(s, type);
Note that this class extends from the ColumnFixture class, which is a subclass of Fixture provided in the Fit libraries and does the work of binding the data from the table to the fields and methods in your class. When writing a ColumnFixture class all you have to do is provide a public field for each input column in your table and a public() method for each output column.

When you execute the test for this table the fixture will iterate over each row of the table, binding each input field to a public instance variable. This binding involves converting the value in the table into the necessary type and reflectively setting the field to the converted value. After the input fields have been set, the ColumnFixture executes the public() method that matches the name of each output column and compares the return value to the value from the table. If all of the return values match the expected values, the row will be displayed in green; if there are any that don't match, the row will turn red.

The base classes provided by Fit do automatic conversions from the strings provided in a test table to the types needed by the Fixture class for setting fields and doing comparisons on the return values of methods. Fit has a default conversion implementation for all the primitive types, their wrapper classes, the String, Date, and ScientificDouble classes, as well as arrays of any of these types. If, however, you wish to define a custom conversion for these or any other classes, you can do so by overriding the parse() method of your fixture. This override is demonstrated in the RentalRates fixture class.

Note that a custom parse() method had to be implemented because the car type field is a custom type that isn't supported by Fit. It uses the static CarType.valueOf() method to convert from a string into an instance of the typesafe enumeration. You can follow a similar pattern for defining a mapping from a string to any Java class.

FitNesse Test
You need to create a new page called TestExample to run the test table in FitNesse. Simply click the Edit Locally link on the left side of the FitNesse front page, scroll to the bottom of the text area, and type the wiki word TestExample. Then save the page. You will return to the front page, and the TestExample link you created will appear at the bottom with a small question mark next to it. Click the question mark to create the new page. You'll switch immediately to the new TestExample page's edit view. Add this wiki markup to the page body, and click Save:

!path fitnesse.jar
!path [Path to fitnesse-examples.jar]

|age|car type|booking method|rental allowed?|daily rate?|
|25 |Economy |Phone         |True           |15.00      |
|25 |Economy |Internet      |True           |13.50      |
|24 |Economy |Phone         |True           |16.50      |
|40 |Standard|Phone         |True           |20.00      |
|25 |Standard|Internet      |True           |18.00      |
|30 |Exotic  |Phone         |True           |45.00      |
|29 |Exotic  |Phone         |False          |           |
|17 |Standard|Internet      |False          |           |
|18 |Standard|Internet      |True           |20.00      |
The first two lines of this wiki code example configure the classpath that FitNesse will use to run this test page. You should modify the second line to give the fully qualified path to the JAR example that is provided. Alternatively, you can also download the source for the examples and set the path element to point to the target directory of the provided build. This procedure is very useful when running FitNesse locally, as changes made in your workspace will be reflected immediately when running tests.

Because the name of this page begins with the word "Test," a test link will appear at the top of the navigation bar on the right. Click the test link, and FitNesse will execute the table and report the results.

As you can see from this example, FitNesse uses the pipe character to represent cell boundaries and new lines to delimit rows. For the sake of readability the cells in this example have been padded with spaces to force the columns to line up properly, but this additional white space is completely optional and will be ignored when the page is rendered.

Note also that the first row of the test table is preceded with an exclamation point that marks the table as a test and tells Fit to execute it using the Fixture class defined in the first row.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date