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.

where to place your test code<br />

Remember, as<br />

long as you’re<br />

using the test<br />

DBAccessor<br />

this is just a<br />

placeholder.<br />

The test wants<br />

a zero-balance<br />

gift card at<br />

the end. So we<br />

simulate that.<br />

Hmm, this isn’t<br />

good; this is what<br />

the test wants but<br />

we’re obviously going<br />

to have to revisit<br />

this. We’ll need<br />

another test.<br />

298 Chapter 8<br />

}<br />

Getting to Green...again.<br />

Now you’ve got a way to isolate the OrderProcessor class from the<br />

database. Implement the processOrder() method using the right<br />

database strategy.<br />

// existing code<br />

private DBAccessor dbAccessor;<br />

public void setDBAccessor(DBAccessor accessor) {<br />

dbAccessor = accessor;<br />

}<br />

public Receipt processOrder(OrderInformation orderInfo) {<br />

}<br />

GiftCard gc = dbAccessor.getGC(orderInfo.getGiftCardNumber());<br />

dbAccessor.saveOrder(orderInfo);<br />

// This is what our test is expecting<br />

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

dbAccessor.saveGC(gc);<br />

Receipt receipt = new Receipt();<br />

receipt.setConfirmationNumber(12345);<br />

receipt.setPickupTime(new Date());<br />

receipt.setGCBalance(gc.getBalance());<br />

return receipt;<br />

Q: I just don’t buy it. We just wrote a bunch <strong>of</strong> code that we<br />

know is wrong. How is this helping me?<br />

A: The test we wrote is valid—we need that test to work. The code<br />

we wrote makes that test work so we can move on to the next one.<br />

That’s the principle behind TDD—just like we broke stories into tasks<br />

Download at WoweBook.Com<br />

Remember, this is just the code<br />

needed to get our test passing; it’s<br />

OK that we’re going to have to<br />

revisit this code for the next test.<br />

to get small pieces, we’re breaking our functionality into small code<br />

pieces. It didn’t take long to write the code to get the first test to pass<br />

and it won’t take long to refactor it to get the second one to pass, or<br />

the third. When you’re finished you’ll have a set <strong>of</strong> tests that makes<br />

sure the system does what it needs to, and you won’t have any more<br />

code than necessary to do it.

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

Saved successfully!

Ooh no, something went wrong!