01.02.2013 Views

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Keep your test code with your tests<br />

All that’s left is to write up an implementation <strong>of</strong> DBAccessor for the<br />

processOrder() method to use, and finish the testSimpleOrder()<br />

test method. But the test implementation <strong>of</strong> DBAccessor is really only used<br />

for tests, so it belongs with your testing classes, not in your production code:<br />

public class TestOrderProcessing {<br />

// other tests<br />

}<br />

public class TestAccessor implements DBAccessor {<br />

public GiftCard getGC(int gcId) {<br />

GiftCard gc = new GiftCard();<br />

gc.setActivationDate(new Date());<br />

gc.setExpirationDate(new Date());<br />

gc.setBalance(new BigDecimal(100));<br />

}<br />

// ... the other DBAccessor methods go here...<br />

}<br />

@Test<br />

public void testSimpleOrder() {<br />

// First create the order processor<br />

OrderProcessor orderProcessor = new OrderProcessor();<br />

orderProcessor.setDBAccessor(new TestAccessor());<br />

}<br />

// Then we need to describe the order we’re about to place<br />

OrderInformation orderInfo = new OrderInformation();<br />

orderInfo.setCustomerName(“Dan”);<br />

orderInfo.setDrinkDescription(“Bold with room”);<br />

orderInfo.setGiftCardNumber(12345);<br />

orderInfo.setPreferredStoreNumber(123);<br />

// Hand it <strong>of</strong>f to the order processor and check the receipt<br />

Receipt receipt = orderProcessor.processOrder(orderInfo);<br />

assertNotNull(receipt.getPickupTime());<br />

assertTrue(receipt.getConfirmationNumber() > 0);<br />

assertTrue(receipt.getGCBalance().equals(0));<br />

Download at WoweBook.Com<br />

All this code is in our test class,<br />

which is in a separate directory from<br />

production code.<br />

test-driven development<br />

Here’s a simple DBAccessor<br />

implementation that<br />

returns the values we want.<br />

Since this is only used for<br />

testing, it’s defined inside<br />

our test class.<br />

Set the OrderProcessor<br />

object to use the test<br />

implementation for<br />

database access—which<br />

means no real database<br />

access at all.<br />

With the testing<br />

database accessor,<br />

we can test this<br />

method, even<br />

without hitting a<br />

live database.<br />

Remember, this was all about the<br />

simplest code possible to return the<br />

expected values here.<br />

you are here 4 299

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!