<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Progressive Development : mock objects</title><link>http://blogs.msdn.com/progressive_development/archive/tags/mock+objects/default.aspx</link><description>Tags: mock objects</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Motley says: "If-then-else hooks are the best way to inject mock objects"</title><link>http://blogs.msdn.com/progressive_development/archive/2007/08/14/motley-says-if-then-else-hooks-are-the-best-way-to-inject-mock-objects.aspx</link><pubDate>Tue, 14 Aug 2007 18:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4363738</guid><dc:creator>James Waletzky</dc:creator><slash:comments>12</slash:comments><comments>http://blogs.msdn.com/progressive_development/comments/4363738.aspx</comments><wfw:commentRss>http://blogs.msdn.com/progressive_development/commentrss.aspx?PostID=4363738</wfw:commentRss><description>&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri; TEXT-DECORATION: underline"&gt;Summary&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley:&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;A simple if-then-else hook is the simplest and best way to inject a mock object.&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: First, you have to design to interfaces. Then, techniques like dependency injection, factories, and endo-testing are more effective ways to inject mock objects.&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;______________________________&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;[Context:&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Maven and Motley continue a discussion on mock objects. Maven is just about to show Motley how to design his classes to be mockable]&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: As I said, a simple if-then-else hook in my code is probably the best way to inject a mock object for testing. It's simple, which is one of your key design principles, right?&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: I'll give you that - it's simple on the outside, but what about maintaining that code into the future? If-then-else hooks all over the place end up looking like spaghetti when you add more over time. Plus, there are other disadvantages.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: First you tell me simple rules, and then you tell me simple leads to spaghetti. You know, Mave, sometimes I want to give you a shot in the-&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Whoa! Stow your temper for a bit, bud. Simplicity, by default, is good. However, there are other slightly m ore elegant solutions that are still simple to follow and maintain. The "if" hooks, although simple to start, lead to complexity. They also require us couple test code to our production code if we inline the "if" statements. Both of those are far from optimal.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Ok, let's hear about your so-called "elegant" solutions.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Let's do it! Each require a little bit of object-oriented knowledge, but you're an expert, so no problem. &lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Ah, you flatter me. You're also right.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Above everything else, as we discussed in the past, you need to design to interfaces. Once you have well defined interfaces between your component boundaries, you can plug and play different implementations - mock objects are a great example. But, that's not all. You need to employ a simple technique to invoke the mock objects when required. The first technique is known as "dependency injection."&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Sounds like something a drug addict would do! &lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Funny guy. Anyway, let's say we are developing an account class for a bank, and that we are doing some kind of transfer service from one account at our bank to some other remote bank over a secure web service. Perhaps we have code like this:&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;public class Account&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;public void Transfer(Money amount)&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;remoteBank.Transfer(amount);&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Pretty straight-forward. Assume that remoteBank talks to a web service somewhere over the wire. We discussed that good unit tests cannot access the network - they have to remain isolated and fast. So, we need to replace the remote bank with something else. We could take your way of putting in an "if" hook, but let's use dependency injection. The remote bank needs a well-defined interface. Assuming that exists, here's what we do:&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;public class Account&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;public void Transfer(IRemoteBank bank, Money amount)&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;bank.Transfer(amount);&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Great. The code basically looks like the same. Are you sniffing something?&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Ah, it may look similar, but it's not the same. A client of the Account class can now pass in an implementation of the remote bank that implements the IRemoteBank interface. That implementation can be a real remote bank (probably by default) or a mock.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: I see some advantages, but jeez, now clients have to know about remote banks, which is arguably an implementation detail of the Account class. &lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Very astute, my friend! Definitely a disadvantage mixed in with the advantages. Another technique is to use the factory pattern to return us the right implementation for our needs. Check this out:&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;public class Account&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;public void Transfer(Money amount)&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;IRemoteBank bank = BankFactory.CreateRemoteBank();&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;bank.Transfer(amount);&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;public class BankFactory&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;static public IRemoteBank CreateRemoteBank()&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;if (someCondition)&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Lucida Console'"&gt;return new MockRemoteBank();&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;else&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Lucida Console'"&gt;return new RemoteBank();&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;The "someCondition" in this case could be a flag or a setting from a configuration file that is true when tests are executing.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Nice, to some extent. We've now made it such that clients don't need to know anything about implementation detail, but you still have your test code coupled to your core business logic. Ideally we'd like to get rid of that.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: I agree! Factories are nice to separate creation from usage, but they don't solve all our problems. This next technique, called Endo-testing, takes care of some of those disadvantages. Here's the code:&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;public class Account&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;public void Transfer(Money amount)&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;IRemoteBank bank = this.createRemoteBank();&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;protected virtual IRemoteBank createRemoteBank()&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 1.125in; FONT-FAMILY: 'Lucida Console'"&gt;return new RemoteBank();&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.75in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 9pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: 'Lucida Console'"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Big deal. So you have an extra helper method to create a new instance of the remote bank object. What does that solve?&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Ah, but notice the &lt;SPAN style="FONT-STYLE: italic"&gt;virtual&lt;/SPAN&gt; on the method declaration. That means that I can subclass the account object, override the createRemoteBank() method on the subclass, and return a mock object for test purposes. &lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Oh, um, I was just testing you. You can definitely override that method for test purposes. I guess that has the advantage that our test code is completely decoupled from our product code and clients still don't need to know about the implementation details.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: You bet! There is a disadvantage though-&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Yeah! You can't seal your classes!&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Yes. We have to allow subclassing from our class even though we may not want to. And, we have a "protected" method, which for documentation purposes is essentially public. We really should document it.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Those seem like small disadvantages though relative to the alternatives. It's a bit more complicated, though, because you have to understand polymorphism, but a good OO developer can deal with that.&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;______________________________&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;&lt;SPAN style="FONT-WEIGHT: bold; COLOR: navy"&gt;Maven's Pointer:&lt;/SPAN&gt; Know any more good techniques for injecting mock objects? Let us know with a comment below. Of course, all this requires that you design to interfaces, which was one of the main design principles discussed previously.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;&lt;SPAN style="FONT-WEIGHT: bold; COLOR: navy"&gt;Maven's Resources:&lt;/SPAN&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;UL style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in; MARGIN-LEFT: 0.375in; DIRECTION: ltr; unicode-bidi: embed" type=circle&gt;
&lt;LI style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle"&gt;&lt;SPAN style="FONT-SIZE: 11pt; FONT-STYLE: italic; FONT-FAMILY: Calibri"&gt;Working Effectively with Legacy Code&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri"&gt;, by Michael Feathers, Prentice Hall PTR, ISBN: 0131177052, Sept. 2004.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4363738" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/progressive_development/archive/tags/unit+testing/default.aspx">unit testing</category><category domain="http://blogs.msdn.com/progressive_development/archive/tags/mock+objects/default.aspx">mock objects</category></item><item><title>Motley says: "The only thing I'm going to mock is you"</title><link>http://blogs.msdn.com/progressive_development/archive/2007/08/07/motley-says-the-only-thing-i-m-going-to-mock-is-you.aspx</link><pubDate>Tue, 07 Aug 2007 20:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4273455</guid><dc:creator>James Waletzky</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/progressive_development/comments/4273455.aspx</comments><wfw:commentRss>http://blogs.msdn.com/progressive_development/commentrss.aspx?PostID=4273455</wfw:commentRss><description>&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri; TEXT-DECORATION: underline"&gt;Summary&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley:&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;I can't mock something that I have no control over (like third-party or old legacy code)&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Design to interfaces, create a wrapper, and use tools for mocking. These are all great practices for leveraging mock objects.&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;______________________________&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;[Context:&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Motley is struggling with creating mock objects for his unit tests]&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Arrrrggggghhhh. You know, I'm really trying to do this TDD thing but it is quickly becoming a real pain in the butt. You said that tests have to execute quick. Trust me, I totally believe you on that front. I'm doing my best to make them execute quickly but some things just are not made to mock.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: What's the problem? &lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: I'm interfacing with Marty's code. I "new" the object up and call it. Unfortunately Marty's stuff takes quite a few seconds to execute. It's making my TDD experience as painful as looking at that shirt you're wearing.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: What's wrong with my shirt? Anyway, the problem is that you are hard coding the instance creation of Marty's object. You are depending on an implementation, which is a violation of one of our key design principles we talked about.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Yeah, yeah, design to interfaces. But Marty has defined the interface and I have no control over it. No stubbing here, bubba!&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Ah, but you do have some control! The best thing to do would be to collaborate with Marty and get him to expose a well-defined and documented public interface to his component. Then you can simply couple yourself to the interface and not the implementation.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Right! Then I can plug and play different implementations of the component, replacing Marty's version with my own stub.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: You betcha! You got it, Mot! &lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: But let's say Marty's component is actually a third party component that I have no control over.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: You can solve &lt;SPAN style="FONT-STYLE: italic"&gt;any&lt;/SPAN&gt; software problem with an extra layer of indirection. I'm kidding of course, but it does help here. In this case, wrap that component with a façade design pattern that provides an interface that you define. Then your core code depends on that façade interface instead of directly on the third party component. &lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Ok, fortunately that makes sense. You used the word "mock" previously but it just seems like a stub to me. What is the difference?&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Don't worry too much about the nomenclature. A stub provides some sort of canned response to a call. Usually the response is hard-coded and there is no real logic associated with the result that is returned. A mock is similar to a stub, but it also verifies some kind of behavior. Did the client of the mock call it appropriately? Is the state of the object as we expect? Are return values from methods that we expected to be called set appropriately? The term "mock" goes along with TDD quite frequently, but you can get by effectively just using a stub if you wish.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: So a mock does extra work on top of a stub. How do I get the mock to do this extra work of behavior verification? Do I have to write all the code myself?&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: You can, but you can also use a mock object framework. There are a few out on the Internet for various platforms. NMock is an example for .NET, but they're out there for other languages too. Generally the name is xMock, where the "x" is replaced by some kind of letter or phrase corresponding to the language or platform for which it is built.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: I'm still not sold - what about all the wrappers I'll have to write to mock out bits of legacy code that were not designed with interfaces in mind?&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Check out a free tool called &lt;A href="http://www.typemock.com/"&gt;TypeMock.NET&lt;/A&gt;. TypeMock allows you to mock &lt;SPAN style="FONT-STYLE: italic"&gt;any&lt;/SPAN&gt; class in your system, including concrete classes. No changes are required to the production code. TypeMock automatically intercepts calls to the class(es) of your choosing and injects whatever behavior you specify in its place. It does this in one of two ways: (a) by recording expectations of the code in a similar way that you would record a macro; and (b) using reflection to look for calls to expected methods and replace them with alternatives. The key is running an executable that wraps your test harness such that it is able to hook the desired method calls.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Holy crap - that sounds incredibly cool! &lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: About as cool as you are, with all the windows open in the winter.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Ouch. A dig from Mave. Actually, the only thing you have dug is your grave once we leave the office and I get my hands on you. In all seriousness, I can understand the mock story, but I'm still not sure about the best technique for mocking something out. I figured I would just hack in an "if" statement to give me the right object given the scenario - release or test.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Good question. A hacked-in hook could work, but it's not the best option. Why don't we go over some methods of injecting mock objects into your design. Let's go write some code on the whiteboard...&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;______________________________&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;&lt;SPAN style="FONT-WEIGHT: bold; COLOR: navy"&gt;Maven's Pointer:&lt;/SPAN&gt; To take advantage of the full power of mock objects, try a mock object framework like &lt;A href="http://nmock.sourceforge.net/quickstart.html"&gt;NMock&lt;/A&gt;. The Quickstart documentation for NMock contains a simple example of how to use the framework and what is meant by behavior verification.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;&lt;SPAN style="FONT-WEIGHT: bold; COLOR: navy"&gt;Maven's Resources:&lt;/SPAN&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;UL style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in; MARGIN-LEFT: 0.375in; DIRECTION: ltr; unicode-bidi: embed" type=circle&gt;
&lt;LI style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle"&gt;&lt;A href="http://www.martinfowler.com/articles/mocksArentStubs.html"&gt;&lt;SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri"&gt;Mocks Aren't Stubs&lt;/SPAN&gt;&lt;/A&gt;&lt;SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri"&gt;, by Martin Fowler;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle"&gt;&lt;A href="http://www.typemock.com/"&gt;&lt;SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri"&gt;TypeMock.Net&lt;/SPAN&gt;&lt;/A&gt;&lt;/LI&gt;
&lt;LI style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle"&gt;&lt;SPAN style="FONT-SIZE: 11pt; FONT-STYLE: italic; FONT-FAMILY: Calibri"&gt;Working Effectively with Legacy Code&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri"&gt;, by Michael Feathers, Prentice Hall PTR, ISBN: 0131177052, Sept. 2004.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4273455" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/progressive_development/archive/tags/mock+objects/default.aspx">mock objects</category><category domain="http://blogs.msdn.com/progressive_development/archive/tags/test-driven+development/default.aspx">test-driven development</category><category domain="http://blogs.msdn.com/progressive_development/archive/tags/TDD/default.aspx">TDD</category></item><item><title>Motley says: "Unit tests take too long to execute, so don't bother"</title><link>http://blogs.msdn.com/progressive_development/archive/2007/04/04/motley-says-unit-tests-take-too-long-to-execute-so-don-t-bother.aspx</link><pubDate>Wed, 04 Apr 2007 18:38:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2016121</guid><dc:creator>James Waletzky</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/progressive_development/comments/2016121.aspx</comments><wfw:commentRss>http://blogs.msdn.com/progressive_development/commentrss.aspx?PostID=2016121</wfw:commentRss><description>&amp;nbsp; 
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri; TEXT-DECORATION: underline"&gt;Summary&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Motley: &lt;/SPAN&gt;Unit tests take too long to execute. I'm not going to bother.&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;&lt;SPAN style="FONT-WEIGHT: bold"&gt;Maven: &lt;/SPAN&gt;Unit tests need to run quickly - you need to execute them frequently. Mock objects can help.&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;______________________________&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;[Context: Motley has been writing unit tests for a couple of days, but is not sold on developers writing test code]&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: I've been trying this unit testing thing for a couple of days now but I can't really say I like it. I developed this little executable to run my tests and that took time. You stated that I should run them regularly, but they just take too long to run.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Whoa - hold on a second. Let's address these things in turn. You're coding using C#, right? &lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Yeah. I wouldn't have it any other way.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Well, it just so happens there are some existing unit test frameworks out there that you should leverage.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Framework? For what? I just wrote a little executable to run my tests.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: A good C# unit test framework like Visual Studio 2005 or NUnit can do some work for you. They quickly run tests, provide some good visual feedback, tell you the details about any failures, and make it dead simple to write new tests. You definitely need to be using a unit test framework!&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: I didn't even know you could do that with Visual Studio - might be worth a try. BUT, these tests just take too long to run so I'm not even sure it's worth the effort.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: There are certain rules of unit testing that you need to follow. First, tests need to run quickly! If they don't run quickly, like in the order of seconds, developers won't run them and they get stale and unused. &lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: But my tests have to run over a network to talk to a web service that has a computational intensive algorithm. It takes time. Nothing I can do.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: You're testing your own code, right? Not the web service on the other end?&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Yeah, so?&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Replace the web service! You can create a stub or a mock object to take the place of the web service. The stub simply simulates the web service and returns a result for your test immediately. No network or long computation required.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: That's cheating, dude! I'm not really testing the system then. It's fake!&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Ahhhh, but we're unit testing here. The keyword is unit. You are testing your code that interacts with the web service, not doing a full integration test from end-to-end. Unit tests cover small, isolated chunks of code and simply validate the implementation of those small chunks.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: But I still have to test the web service results!&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: Yes, but the web service will have its own set of unit tests.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;And just for the record, you also want your unit tests to:&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in 0in 0in 0.375in"&gt;&lt;SPAN style="FONT-FAMILY: 'Times New Roman'"&gt;○&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Calibri"&gt; Leave the system in the same state you found it (e.g. don't leave files sitting around that your test created)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in 0in 0in 0.375in"&gt;&lt;SPAN style="FONT-FAMILY: 'Times New Roman'"&gt;○&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Calibri"&gt; Test very small chunks and not combine test cases (helps you quickly pinpoint the problem code)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in 0in 0in 0.375in"&gt;&lt;SPAN style="FONT-FAMILY: 'Times New Roman'"&gt;○&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Calibri"&gt; Cover positive cases (the main logic) as well as negative logic (error cases)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in 0in 0in 0.375in"&gt;&lt;SPAN style="FONT-FAMILY: Calibri"&gt;&lt;SPAN style="FONT-FAMILY: 'Times New Roman'"&gt;○&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Calibri"&gt; Not be dependent on each other (don't expect unit tests to run in a particular order)&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in 0in 0in 0.375in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Motley: Okay, I'll try your suggestions. I guess if I want to replace components with stubs, I need to make sure they have well-defined interfaces so I can plug and play different implementations.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;Maven: You are a smart man, Mot, a smart man.&lt;/P&gt;
&lt;P style="FONT-WEIGHT: bold; FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;______________________________&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;&lt;SPAN style="FONT-WEIGHT: bold; COLOR: navy"&gt;Maven's Pointer:&lt;/SPAN&gt; Check out the infrastructure components that already exist to help with unit testing. Free tools for managed code include NUnit (&lt;A href="http://www.nunit.org/" mce_href="http://www.nunit.org/"&gt;http://www.nunit.org/&lt;/A&gt;)&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;as a test framework and NMock (&lt;A href="http://nmock.org/" mce_href="http://nmock.org/"&gt;http://nmock.org/&lt;/A&gt;) for creating mock objects. &lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri"&gt;&lt;SPAN style="FONT-WEIGHT: bold; COLOR: navy"&gt;Maven's Resources:&lt;/SPAN&gt; &lt;SPAN style="FONT-STYLE: italic"&gt;Pragmatic Unit Testing in C# with Nunit&lt;/SPAN&gt;, by Andy Hunt and Dave Thomas, Pragmatic Bookshelf, ISBN: 0977616673, 2007.&lt;/P&gt;
&lt;P style="FONT-SIZE: 11pt; MARGIN: 0in; FONT-FAMILY: Calibri" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;Temporary Link: &lt;A href="http://technorati.com/claim/575hu433ub" rel=me&gt;Technorati Profile&lt;/A&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2016121" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/progressive_development/archive/tags/unit+testing/default.aspx">unit testing</category><category domain="http://blogs.msdn.com/progressive_development/archive/tags/mock+objects/default.aspx">mock objects</category></item></channel></rss>