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.

}<br />

test-driven development<br />

// Tell our test framework what to call, and what to expect<br />

EasyMock.expect(mockAccessor.getGC(12345)).andReturn(startGC);<br />

// Simulate processing an order<br />

mockAccessor.saveOrder(orderInfo);<br />

// Then the processor should call saveGC(...) with an empty GC<br />

mockAccessor.saveGC(endGC);<br />

// And nothing else should get called on our mock.<br />

EasyMock.replay(mockAccessor);<br />

// Create an OrderProcessor...<br />

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

processor.setDBAccessor(mockAccessor);<br />

Receipt rpt = processor.processOrder(orderInfo);<br />

// Validate receipt...<br />

Once you create a mock object, it’s in “record<br />

mode.” That means you tell it what to expect and<br />

what to do...so when you put it in replay mode,<br />

and your tests use it, you’ve set up exactly what<br />

the mock object should do.<br />

First, expect a call to getGC() with the value<br />

12345... that matches up with the orderInfo<br />

object we created over here.<br />

This might seem like a good bit <strong>of</strong> work<br />

here, but we’ve saved one class. Add in<br />

all the other variations <strong>of</strong> testing for<br />

specific gift card things, and you’ll save<br />

lots <strong>of</strong> classes...and that’s a big deal.<br />

Download at WoweBook.Com<br />

Remember, you<br />

haven’t had to<br />

write your own<br />

class... that’s<br />

the big win here.<br />

When getGC() is called with that<br />

value, return the startGC object... this<br />

simulates getting a card from the<br />

database, and we’ve supplied the exact<br />

values we want for this test scenario.<br />

This doesn’t do anything...but it tells the mock<br />

object that you should have saveOrder() called, with<br />

orderInfo as the parameter. Otherwise, something’s<br />

gone wrong, and it should throw an exception.<br />

Then, the mock object should have saveGC() called on it, with the<br />

endGC gift card simulating the right amount <strong>of</strong> money being spent.<br />

If this isn’t called, with these values, then the test should fail.<br />

Calling replay() tells the mock object<br />

framework “OK, something is going to<br />

replay these activities, so get ready.” This is like<br />

activating the<br />

object; it’s ready<br />

to be used now.<br />

And here’s where we use the mock<br />

object as a stand-in for a DBAccessor<br />

implementation: we test order processing,<br />

never having to write a custom<br />

implementation for this particular test<br />

case (or for any <strong>of</strong> the other specific<br />

test cases we need to check out).<br />

you are here 4 307

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

Saved successfully!

Ooh no, something went wrong!