22.2. Changing State of Chat Room The chat room tests, from Section 4.2 on p. 26, are shown again in Figure 22.5. The fixture code for this is shown in Listing 22.2. Figure 22.5. Fit Table for Testing Chat Room Changesfit.ActionFixture | start | ChatServerActions | enter | user | anna | press | connect | enter | room | lotr | press | new room | press | enter room | enter | user | luke | press | connect | press | enter room | check | occupant count | 2 |
Listing 22.2. ChatServerActions.java public class ChatServerActions extends fit.Fixture { private ChatRoom chat = new ChatRoom(); private String userName = ""; private String roomName = ""; public void user(String userName) { this.userName = userName; } public void connect() { chat.connectUser(userName); } public void room(String roomName) { this.roomName = roomName; } public void newRoom() { chat.userCreatesRoom(userName, roomName); } public void enterRoom() { chat.userEntersRoom(userName,roomName); } public int occupantCount() { return chat.occupants(roomName); } } The operation of ActionFixture on this table is much the same as in the previous example. Each of the actions in the rows of the table is carried out in turn, starting with the second row. The start action in the second row of this table creates an object of the named class ChatServerActions. This is the actor, which in turn starts the system under test by creating a ChatRoom object, as shown in the code in Listing 22.2. The enter (user) action in the third row calls the named method user() of the actor, passing to it the String value anna given in the third cell. This sets up the fixture value in userName for the next action. The press (connect) action in the fourth row calls the named method connect() of the actor. This in turn calls the method connect() of the system under test, passing the current userName as parameter. The enter (room) action in the fifth row calls the named method room() of the actor, passing to it the value lotr given in the third cell. This sets up the fixture value in roomName for the following action. The press (new room) action in the sixth row calls the named method newRoom() of the actor. This in turn calls the method userCreatesRoom() in the system under test, passing the current userName and roomName as parameters. The method name in the table is in camel casing (see the Note), so new room is treated as the method name newRoom(). The press (enter room) action in the seventh row calls the named method enterRoom() of the actor. This in turn calls the method enterRoom() in the system under test, passing the current userName and roomName as parameters. And so on. The final row of the table has the action check, which calls occcupantCount() of the actor and checks that the returned value is 2. The actions enter, press, and check require that a public method with the right arguments and an appropriate return type exist in the actor. Note Camel casing, as introduced in the Note on 183, is used to remove spaces in the names in the enter, press, and check actions in ActionFixture tables in order to produce a valid Java identifier. |