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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

keep it simple<br />

You can just make<br />

up a gift card<br />

number here...<br />

The simplest thing<br />

here is to not worry<br />

about the balance<br />

on the card... this<br />

is just testing the<br />

simplest version <strong>of</strong><br />

order processing.<br />

292 Chapter 8<br />

Red<br />

Your job was to implement a test that will verify your s<strong>of</strong>tware can<br />

process a simple order.<br />

// existing tests<br />

@Test<br />

public void testSimpleOrder() {<br />

}<br />

// First create the order processor<br />

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

// Then you need to describe the order that should be placed<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 the order <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 />

Q: How can you just assume that the gift card has the right<br />

amount on it? Isn’t that an assumption? Aren’t those bad?<br />

A: We’re writing our first test, and then we need to make it pass.<br />

So, we’re sort <strong>of</strong> assuming that the gift card has enough on it, but<br />

since we’re about to implement the backend code, we can make<br />

sure it does then. What we are setting ourselves up for is some<br />

refactoring. Once we get this test passing we’ll obviously need to add<br />

a test for a gift card that doesn’t have enough money on it. When we<br />

do that, we’ll certainly have to revisit the code we wrote to get this<br />

test going and rework it to support different gift cards and different<br />

values. But, this is going to take some thought. Read on...<br />

Download at WoweBook.Com<br />

Q: There are a bunch <strong>of</strong> values in that test that aren’t<br />

constants—should I care?<br />

A: Yes, you should. To keep the code sample short we didn’t<br />

pull those values into constants, but you should treat your test<br />

code just like production code you write and apply the same style<br />

and discipline. Remember, this isn’t throwaway code; it lives in the<br />

repository with the rest <strong>of</strong> your system, and you rely on it to let you<br />

know if things aren’t working right. Treat it with respect.

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

Saved successfully!

Ooh no, something went wrong!