Section 23.1. Testing Unordered Lists


23.1. Testing Unordered Lists

Let's now return to the chat room occupant test from Section 5.1 on p. 31, as shown again in Figure 23.1. This is a RowFixture table, with the header row labeling the fields that make up the elements of the list.

Figure 23.1. Fit Table for Testing Occupants

OccupantList

user

room

anna

lotr

luke

lotr


The application has a list of rooms, each of which has a list of users occupying that room. The fixture code needs to collect the data from the system under test and create a local collection specifically for RowFixture to use in matching against the rows of the table. Figure 23.2 shows this, with the Room and User objects from the system under test at the bottom and the Occupancy objects created from them by the fixture to the right.

Figure 23.2. Fit Runs the Table


The fixture code is shown in Listing 23.1. As a subclass of fit.RowFixture, OccupantList defines the two methods query() and getTargetClass(). The method query() returns an array of elements of the actual collection; the method getTargetClass() returns the class of the elements of the array. The method query() of the fixture (line 4 in Listing 23.1) assembles the elements of the actual list from the List of Room supplied by the ChatServer, the system under test.

The appropriate data is assembled into an array of Occupancy in the query() method in Listing 23.1. As shown in Listing 23.2, the target class is Occupancy, which has public instance variables for user and room, corresponding to the labels in the header row in Figure 23.1.

Listing 23.1. OccupantList.java
 1 public class OccupantList extends fit.RowFixture { 2    private ChatServer chat = new ChatServer(); 3 4    public Object[] query() throws Exception { 5       List occupancies = new ArrayList(); 6       for (Iterator it = chat.getRooms(); it.hasNext(); ) { 7          Room room = (Room)it.next(); 8          collectOccupants(occupancies, room); 9       } 10      return occupancies.toArray(); 11   } 12   public Class getTargetClass() { 13      return Occupancy.class; 14   } 15   private void collectOccupants(List occupancies, Room room) { 16      for (Iterator it = room.users(); it.hasNext(); ) { 17         User user = (User)it.next(); 18         Occupancy occupant = new Occupancy(room.getName(), 19                                        user.getName()); 20         occupancies.add(occupant); 21      } 22   } 23 } 

Listing 23.2. Occupancy.java
 public class Occupancy {    public String room, user;    public Occupancy(String roomName, String userName) {       this.room = roomName;       this.user = userName;    } } 

Inherited by OccupantList, the doTable() code in fit.RowFixture has four major steps to carry out:

1.

Call the method getTargetClass() of the fixture to determine the class of the elements of the actual list (Occupancy)

2.

Gather up the expected list from the table

3.

Call the method query() of the fixture to gather up the actual list, which is assembled in this case with data from Room and User objects in the system under test

4.

Compare the actual and expected lists and mark both the test rows green to show that the actual list matches the two expected elements

Questions & Answers

Q1:

Can RowFixture test an array of int?

A1:

No, it can handle only an array of Object. The fixture could wrap up an array of int into an array of objects, where each object has a single public instance variable of type int. An easier alternative would be to handle it as a list of values in a single cell, as shown in Section 3.4 on p. 19.

Q2:

Why does query() have tHRows Exception?

A2:

In general, the method in the RowFixture subclass may fail with an exception for some reason, such as when connecting to a database.



    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