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.

A mock object stands in for real objects<br />

There’s really no need for three different accessors, all <strong>of</strong> which create a new GiftCard<br />

object and populate it with different data. That’s a lot <strong>of</strong> extra code to instantiate a<br />

GiftCard and call some setter methods.<br />

Since we have an interface that describes what each <strong>of</strong> these implementations should look<br />

like, we can take advantage <strong>of</strong> a mock object framework to do the heavy lifting. Instead<br />

<strong>of</strong> implementing all <strong>of</strong> the classes ourselves, we can give the framework the interface we<br />

want implemented and then tell it what we expect to happen.<br />

Here’s a strategy<br />

that will make good<br />

gift cards...<br />

TestGoodDBAccessor<br />

+ getGC(gcId : int) :GiftCard<br />

+ saveGC(card : GiftCard) :void<br />

<br />

DBAccessor<br />

+ getGC(gcId : int) :GiftCard<br />

+ saveGC(card : GiftCard) :void<br />

TestInsufficientDBAccessor<br />

+ getGC(gcId : int) :GiftCard<br />

+ saveGC(card : GiftCard) :void<br />

Here’s one that will<br />

make a gift card with<br />

TestInvalidDBAccessor<br />

+ getGC(gcId : int) :GiftCard<br />

+ saveGC(card : GiftCard) :void<br />

test-driven development<br />

This is really what our Order Processor<br />

depends on—the DBAccessor interface.<br />

That’s what we’re going to “mock up”<br />

rather than write real classes...<br />

And our real,<br />

MySQL one.<br />

MySQLDBAccessor<br />

+ getGC(gcId : int) :GiftCard<br />

+ saveGC(card : GiftCard) :void<br />

insufficient funds on it...Here’s one that will throw<br />

We really don’t want to have to write<br />

and maintain these. Instead we’ll<br />

replace all <strong>of</strong> them with mock objects.<br />

The mock framework will handle creating implementations <strong>of</strong> the interface and keeping track<br />

<strong>of</strong> what methods we say should be called, what they should return when they are called, what<br />

shouldn’t be called, etc. The mock framework’s implementation <strong>of</strong> our interface will track all <strong>of</strong><br />

this and throw an error if something doesn’t go according to the plan we gave it.<br />

* We’re going to use the EasyMock framework here but a mock object framework exists<br />

for most languages and they all work similarly.<br />

Download at WoweBook.Com<br />

an error regardless <strong>of</strong><br />

what ID you give it.<br />

you are here 4 305

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

Saved successfully!

Ooh no, something went wrong!