35.2. Testing IndirectlyAnother approach is to carry out, through the API, actions that emulate the user's actions. This approach has the advantage of checking that the calculation business rules are handled in the workflow. This may be the only way if the corresponding logic is awkwardly embedded and if test support is needed to untangle it in order to later test it directly. Part of the test from Chapter 16 is shown in Figure 35.2. (The fixture name in the initial table is altered, however, so that it can make use of the fixtures that were developed for other Fit tests in the past few chapters.) Figure 35.2. Testing Through the ApplicationThe action in the first row of the second table results in a call of the method refundDollarPerHourPerDayPerWeek() of the StartApplication object, as shown in Listing 35.3. This creates temporary client and staff objects; makeDummyClient() in Listing 35.3, for example, is used by a variety of such tests. This method also creates a rental item object with the appropriate charge rates to use. It returns a Refunder CalculateFixture object, which is used for the rest of the table rows. The CalculateFixture class Refunder is shown in Listing 35.4. For each row of the second table, from the third row on, the method refundPaidTimeActualTime() is called with two Durations. The refundPaidTimeActualTime() method makes use of TRansActionFixture methods to carry out the operations, rather than duplicating code. The method creates a start date and an end date, with the given duration between them. With the first transaction, created with the start dateequivalent to the action time is nowit rents an item until the end date, pays the cost, and completes the transaction. Listing 35.3. StartApplication.javapublic class StartApplication extends DoFixture { private RentEz rentEz; public Fixture refundDollarPerHourPerDayPerWeek(Money perHour, Money perDay, Money perWeek) throws Exception { StaffMember staff = makeDummyStaff(); Client client = makeDummyClient(); RentalItemType item = makeDummyRentalItem( new Rates(perHour, perDay, perWeek)); return new Refunder(rentEz,staff,client,item.getName()); } private RentalItemType makeDummyRentalItem(Rates rates) { final String name = "dummy-rental"; final int count = 1; final Money bond = new Money(0); rentEz.removeRentalItemType(name); rentEz.createRentalItemType(name,count,rates,bond); return rentEz.getRentalItemType(name); } private Client makeDummyClient() throws RpsException { final String name = "dummy-client"; try { return getClient(name); } catch (MissingException e) { rentEz.createClient(rentEz.getAdminStaff(), name, "phone"); return getClient(name); } } // ... } The second transaction is created with a date corresponding to the end of the actual duration. Then the rental item is returned, the refund is made, and the transaction is completed. The method returns the refund amount. Questions & Answers
|