Section 34.1. Introduction


34.1. Introduction

Emily and Neo worked together to implement the fixtures for the tests in Chapter 15.

Those tests assume that the clock and time can be changed. "However," stressed Neo, "it would be a big mistake to try and fiddle with the real system clock."

"Yes, I agree," replied Emily, "so let's use a mock object for the date and time when running the tests. That will mean changing the application." (See the following Note on mock objects.)

Note

A mock object [MFC00] is designed to be substituted for the usual object in a system to simplify testing and to focus tests on a subset of the system. The use of mock objects is common in test-driven development and is a big topic in its own right [Bec02], [HT03], [Ast03], [RS04].


"At least," agreed Neo, "we can do the changes in small steps, and the tests will pick up any uses of the clock that we miss in RentEz."

Emily suggested, "Let's access the system clock indirectly in the application, through an object of a new class, SystemClock. In order to substitute a mock clock, we'll also need an interface: say, Clock."

She took over the computer and coded the SystemClock class shown in Listing 34.1. "It simply returns information from the system clock: A new Date is set with the current date and time."

Listing 34.1. SystemClock.java
 public class SystemClock implements Clock {    public Date getTime() {       return new Date();    } } 

"The Clock interface is simple, with one method," added Emily. The interface code she entered is shown in Listing 34.2.

Listing 34.2. Clock.java
 public interface Clock {    public Date getTime(); } 

"We need a single way for all the code to access the date and time," Emily offered.

"Yes," replied Neo, "let's have a method in RentEz for that."

Emily altered the application, RentEz, so that it creates a SystemClock as the clock to use, as shown in Listing 34.3.

Listing 34.3. RentEz.java (version 1)
 public class RentEz {    private Clock clock;    public RentEz() {       clock = new SystemClock();    }    public Date getTime() {       return clock.getTime();    }    // ... } 

Neo noted, "So let's change the application code to call the getTime() method of RentEz, rather than accessing the system clock directly."

"Yes," replied Emily, "we can tackle one Fit test at a time and make only the changes that are needed to pass that test."



    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