Questions Answers


Questions & Answers

Q1:

Why did you change it to -1?

A1:

It's a good practice to make sure that the tests all fail initially. Then when you make a change to the code, such as hooking the fixture into the system under test, you see that your tests now pass because of that change.

Emily sketched the classes involved in handling late returns, as shown in Figure 31.2. The code for calculating the late return is in the class Rental, which depends on configuration information that's extracted from the database when the application starts.

Figure 31.2. Emily's Sketch of the Application


What's the best way to hook it into the application? Emily mentioned the following possibilities as she thought of them, with some prompting and encouragement.

"I could call my method inside Rental from the fixture. But it assumes associated RentalItem and Client objects, and that's kind of messy to set up. There needs to be a record in the database for all this stuff, and so on. I'm not sure how to set it up.

"Or I could add a new method, setUpTest(), to Rental, which sets things up in the database before calling the normal method to do the calculation.

"Better still, I could add a new method, exTRaHours(), to Rental, which takes all the information as parameters and returns the result. The fixture can call that directly. But it's odd to have it there if it doesn't use any of the information inside the Rental object!

"It would make more sense as a static method.

"Instead, I'll create a new classsay, LateReturnsthat has a method, extraHours(), for doing those calculations. Then my method in the Rental object can gather up the information and call extraHours(). The calculations for the grace period can go in there, too. Then we can test that directly. We can create an object at startup for this, and it can hold the late returns configuration information."

Emily's fixture is now as shown in Listing 31.2. She's set up class LateReturns so that "it can later be fed all the late returns configuration info."

Listing 31.2. TestLate.java (version 2)
 public class TestLate extends fit.ColumnFixture {    public float hoursLate, grace, highDemand;    public boolean countGrace;    public int extraHours() {       LateReturns lateReturns = new LateReturns(countGrace);       return lateReturns.extraHours(hoursLate,grace,highDemand);    } } 

She was eager to write the new class and change her code. But we suggested that she first write and test a separate LateReturns class.



    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