<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blog.alecl.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>Alec Lazarescu's Blog : Unit Testing</title><link>http://blog.alecl.com/archive/tags/Unit+Testing/default.aspx</link><description>Tags: Unit Testing</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.2)</generator><item><title>Syntactic Sugar for Thrown Exception Checking with Generics</title><link>http://blog.alecl.com/archive/2008/10/25/syntactic-sugar-for-thrown-exception-checking-with-generics.aspx</link><pubDate>Sat, 25 Oct 2008 15:52:00 GMT</pubDate><guid isPermaLink="false">022ae8cf-15ca-4180-8062-b150ad1660bf:31</guid><dc:creator>alecl</dc:creator><slash:comments>1</slash:comments><comments>http://blog.alecl.com/comments/31.aspx</comments><wfw:commentRss>http://blog.alecl.com/commentrss.aspx?PostID=31</wfw:commentRss><description>&lt;p&gt;Using attributes like [ExpectedException] can obscure intent because
they apply to the whole method instead of a specific call you're
checking. To make intent totally clear and prevent unexpected bugs, you should wrap just the statement you're expecting an exception from in a try/catch:&lt;br&gt;&lt;/p&gt;&lt;pre style="border-style:solid;border-width:1px;padding:0.5cm;font-size:10pt;font-family:Courier New;"&gt;&lt;span&gt;int&lt;/span&gt; y &lt;span&gt;=&lt;/span&gt; &lt;span&gt;0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="font-weight:bold;"&gt;try&lt;/span&gt;&lt;br&gt;&lt;span&gt;{&lt;/span&gt;&lt;br&gt;    &lt;span&gt;int&lt;/span&gt; x &lt;span&gt;=&lt;/span&gt; &lt;span&gt;1&lt;/span&gt; &lt;span&gt;/&lt;/span&gt; y&lt;span&gt;;&lt;/span&gt;&lt;br&gt;    Assert&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Fail&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;"Exception not thrown DivideByZeroException"&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;br&gt;&lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;span style="font-weight:bold;"&gt;catch&lt;/span&gt; &lt;span&gt;(&lt;/span&gt;DivideByZeroException&lt;span&gt;)&lt;/span&gt;&lt;br&gt;&lt;span&gt;{&lt;/span&gt;&lt;br&gt;&lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;/pre&gt;&lt;p&gt;In addition, at times the same exception is thrown, but with different descriptive messages and in the interest of checking correctness we need to check the message as well.&amp;nbsp; To help achieve &lt;a href="http://en.wikipedia.org/wiki/DRY_code" target="_blank"&gt;DRY&lt;/a&gt;, clarity, and expressiveness, I've created some helper methods for my unit tests to deal with testing proper exception throwing.&amp;nbsp; Note that to be totally by the book, you likely should be creating
special custom exceptions for each type rather than checking the
message strings, but this can be superfluous at times so the option is
here.&lt;/p&gt;&lt;pre style="border-style:solid;border-width:1px;padding:0.5cm;font-size:10pt;font-family:Courier New;"&gt;&lt;span style="font-weight:bold;"&gt;public class&lt;/span&gt; SpecHelpers&lt;br&gt;&lt;span&gt;{&lt;/span&gt;&lt;br&gt;    &lt;span style="font-weight:bold;"&gt;public static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; ExpectException&lt;span&gt;&amp;lt;&lt;/span&gt;T&lt;span&gt;&amp;gt;(&lt;/span&gt;Action methodCall&lt;span&gt;)&lt;/span&gt; where T &lt;span&gt;:&lt;/span&gt; Exception&lt;br&gt;    &lt;span&gt;{&lt;/span&gt;&lt;br&gt;        &lt;span&gt;bool&lt;/span&gt; caught &lt;span&gt;=&lt;/span&gt; &lt;span style="font-weight:bold;"&gt;false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;br&gt;&lt;br&gt;        &lt;span style="font-weight:bold;"&gt;try&lt;/span&gt;&lt;br&gt;        &lt;span&gt;{&lt;/span&gt;&lt;br&gt;            &lt;span&gt;methodCall&lt;/span&gt;&lt;span&gt;();&lt;/span&gt;&lt;br&gt;        &lt;span&gt;}&lt;/span&gt;&lt;br&gt;        &lt;span style="font-weight:bold;"&gt;catch&lt;/span&gt; &lt;span&gt;(&lt;/span&gt;T&lt;span&gt;)&lt;/span&gt;&lt;br&gt;        &lt;span&gt;{&lt;/span&gt;&lt;br&gt;            caught &lt;span&gt;=&lt;/span&gt; &lt;span style="font-weight:bold;"&gt;true&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;br&gt;        &lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;        &lt;span style="font-weight:bold;"&gt;if&lt;/span&gt; &lt;span&gt;(!&lt;/span&gt;caught&lt;span&gt;)&lt;/span&gt;&lt;br&gt;            Assert&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Fail&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;String&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Format&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;"Expected exception {0} not thrown"&lt;/span&gt;&lt;span&gt;));&lt;/span&gt;&lt;br&gt;    &lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;    &lt;span style="font-weight:bold;"&gt;public static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; ExpectExceptionMessageStartsWith&lt;span&gt;&amp;lt;&lt;/span&gt;T&lt;span&gt;&amp;gt;(&lt;/span&gt;Action methodCall&lt;span&gt;,&lt;/span&gt; &lt;span&gt;string&lt;/span&gt; startsWith&lt;span&gt;)&lt;/span&gt; where T &lt;span&gt;:&lt;/span&gt; Exception&lt;br&gt;    &lt;span&gt;{&lt;/span&gt;&lt;br&gt;        &lt;span&gt;bool&lt;/span&gt; caught &lt;span&gt;=&lt;/span&gt; &lt;span style="font-weight:bold;"&gt;false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;br&gt;&lt;br&gt;        &lt;span style="font-weight:bold;"&gt;try&lt;/span&gt;&lt;br&gt;        &lt;span&gt;{&lt;/span&gt;&lt;br&gt;            &lt;span&gt;methodCall&lt;/span&gt;&lt;span&gt;();&lt;/span&gt;&lt;br&gt;        &lt;span&gt;}&lt;/span&gt;&lt;br&gt;        &lt;span style="font-weight:bold;"&gt;catch&lt;/span&gt; &lt;span&gt;(&lt;/span&gt;T ex&lt;span&gt;)&lt;/span&gt;&lt;br&gt;        &lt;span&gt;{&lt;/span&gt;&lt;br&gt;            &lt;span style="font-weight:bold;"&gt;if&lt;/span&gt; &lt;span&gt;(!&lt;/span&gt;ex&lt;span&gt;.&lt;/span&gt;Message&lt;span&gt;.&lt;/span&gt;&lt;span&gt;StartsWith&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;startsWith&lt;span&gt;,&lt;/span&gt; &lt;span style="font-weight:bold;"&gt;true&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; &lt;span style="font-weight:bold;"&gt;null&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;br&gt;                Assert&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Fail&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;String&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Format&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;"Expected exception {0} thrown, but message didn't start with&lt;/span&gt; &lt;span&gt;\"&lt;/span&gt;&lt;span&gt;{1}&lt;/span&gt;&lt;span&gt;\"&lt;/span&gt;&lt;span&gt;.  Message:&lt;/span&gt; &lt;span&gt;\"&lt;/span&gt;&lt;span&gt;{2}&lt;/span&gt;&lt;span&gt;\"&lt;/span&gt;&lt;span&gt;"&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; &lt;br&gt;                            ex&lt;span&gt;.&lt;/span&gt;&lt;span&gt;GetType&lt;/span&gt;&lt;span&gt;().&lt;/span&gt;FullName&lt;span&gt;,&lt;/span&gt; startsWith&lt;span&gt;,&lt;/span&gt; ex&lt;span&gt;.&lt;/span&gt;Message&lt;span&gt;));&lt;/span&gt;&lt;br&gt;&lt;br&gt;            caught &lt;span&gt;=&lt;/span&gt; &lt;span style="font-weight:bold;"&gt;true&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;br&gt;        &lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;        &lt;span style="font-weight:bold;"&gt;if&lt;/span&gt; &lt;span&gt;(!&lt;/span&gt;caught&lt;span&gt;)&lt;/span&gt;&lt;br&gt;            Assert&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Fail&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;String&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Format&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;"Expected exception {0} not thrown"&lt;/span&gt;&lt;span&gt;),&lt;/span&gt; &lt;span&gt;typeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;T&lt;span&gt;).&lt;/span&gt;FullName&lt;span&gt;);&lt;/span&gt;&lt;br&gt;    &lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;    &lt;span style="font-weight:bold;"&gt;public static&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; ExpectExceptionMessageContains&lt;span&gt;&amp;lt;&lt;/span&gt;T&lt;span&gt;&amp;gt;(&lt;/span&gt;Action methodCall&lt;span&gt;,&lt;/span&gt; &lt;span&gt;string&lt;/span&gt; contains&lt;span&gt;)&lt;/span&gt; where T &lt;span&gt;:&lt;/span&gt; Exception&lt;br&gt;    &lt;span&gt;{&lt;/span&gt;&lt;br&gt;        &lt;span&gt;bool&lt;/span&gt; caught &lt;span&gt;=&lt;/span&gt; &lt;span style="font-weight:bold;"&gt;false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;br&gt;&lt;br&gt;        &lt;span style="font-weight:bold;"&gt;try&lt;/span&gt;&lt;br&gt;        &lt;span&gt;{&lt;/span&gt;&lt;br&gt;            &lt;span&gt;methodCall&lt;/span&gt;&lt;span&gt;();&lt;/span&gt;&lt;br&gt;        &lt;span&gt;}&lt;/span&gt;&lt;br&gt;        &lt;span style="font-weight:bold;"&gt;catch&lt;/span&gt; &lt;span&gt;(&lt;/span&gt;T ex&lt;span&gt;)&lt;/span&gt;&lt;br&gt;        &lt;span&gt;{&lt;/span&gt;&lt;br&gt;            &lt;span style="font-weight:bold;"&gt;if&lt;/span&gt; &lt;span&gt;(&lt;/span&gt;ex&lt;span&gt;.&lt;/span&gt;Message&lt;span&gt;.&lt;/span&gt;&lt;span&gt;IndexOf&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;contains&lt;span&gt;,&lt;/span&gt; StringComparison&lt;span&gt;.&lt;/span&gt;InvariantCultureIgnoreCase&lt;span&gt;) &amp;lt;&lt;/span&gt; &lt;span&gt;0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;br&gt;                Assert&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Fail&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;String&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Format&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;"Expected exception {0} thrown, but message didn't contain&lt;/span&gt; &lt;span&gt;\"&lt;/span&gt;&lt;span&gt;{1}&lt;/span&gt;&lt;span&gt;\"&lt;/span&gt;&lt;span&gt;.  Message:&lt;/span&gt; &lt;span&gt;\"&lt;/span&gt;&lt;span&gt;{2}&lt;/span&gt;&lt;span&gt;\"&lt;/span&gt;&lt;span&gt;"&lt;/span&gt;&lt;span&gt;,&lt;/span&gt; &lt;br&gt;                            ex&lt;span&gt;.&lt;/span&gt;&lt;span&gt;GetType&lt;/span&gt;&lt;span&gt;().&lt;/span&gt;FullName&lt;span&gt;,&lt;/span&gt; contains&lt;span&gt;,&lt;/span&gt; ex&lt;span&gt;.&lt;/span&gt;Message&lt;span&gt;));&lt;/span&gt;&lt;br&gt;&lt;br&gt;            caught &lt;span&gt;=&lt;/span&gt; &lt;span style="font-weight:bold;"&gt;true&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;br&gt;        &lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;        &lt;span style="font-weight:bold;"&gt;if&lt;/span&gt; &lt;span&gt;(!&lt;/span&gt;caught&lt;span&gt;)&lt;/span&gt;&lt;br&gt;            Assert&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Fail&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;String&lt;span&gt;.&lt;/span&gt;&lt;span&gt;Format&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;"Expected exception {0} not thrown"&lt;/span&gt;&lt;span&gt;),&lt;/span&gt; &lt;span&gt;typeof&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;T&lt;span&gt;).&lt;/span&gt;FullName&lt;span&gt;);&lt;/span&gt;&lt;br&gt;    &lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;some example calls:&lt;/p&gt;&lt;pre style="border-style:solid;border-width:1px;padding:0.5cm;font-size:10pt;font-family:Courier New;"&gt;&lt;span style="font-weight:bold;"&gt;private&lt;/span&gt; &lt;span&gt;bool&lt;/span&gt; &lt;span&gt;ThrowEx&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;br&gt;&lt;span&gt;{&lt;/span&gt;&lt;br&gt;    &lt;span style="font-weight:bold;"&gt;throw new&lt;/span&gt; &lt;span&gt;Exception&lt;/span&gt;&lt;span&gt;();&lt;/span&gt;&lt;br&gt;&lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="font-weight:bold;"&gt;private&lt;/span&gt; &lt;span&gt;bool&lt;/span&gt; &lt;span&gt;ThrowExContains&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;br&gt;&lt;span&gt;{&lt;/span&gt;&lt;br&gt;    &lt;span style="font-weight:bold;"&gt;throw new&lt;/span&gt; &lt;span&gt;NullReferenceException&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;"a special message"&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;br&gt;&lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span&gt;[&lt;/span&gt;Test&lt;span&gt;]&lt;/span&gt;&lt;br&gt;&lt;span style="font-weight:bold;"&gt;public&lt;/span&gt; &lt;span&gt;void&lt;/span&gt; &lt;span&gt;TestExceptionWrapper&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;br&gt;&lt;span&gt;{&lt;/span&gt;&lt;br&gt;    &lt;span style="font-style:italic;"&gt;// .NET 3.5 style with lamba notation&lt;/span&gt;&lt;br&gt;    SpecHelpers&lt;span&gt;.&lt;/span&gt;ExpectException&lt;span&gt;&amp;lt;&lt;/span&gt;Exception&lt;span&gt;&amp;gt;(() =&amp;gt;&lt;/span&gt; &lt;span&gt;ThrowEx&lt;/span&gt;&lt;span&gt;());&lt;/span&gt;&lt;br&gt;    SpecHelpers&lt;span&gt;.&lt;/span&gt;ExpectExceptionMessageContains&lt;span&gt;&amp;lt;&lt;/span&gt;NullReferenceException&lt;span&gt;&amp;gt;(() =&amp;gt;&lt;/span&gt; &lt;span&gt;ThrowExContains&lt;/span&gt;&lt;span&gt;(),&lt;/span&gt; &lt;span&gt;"special"&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;br&gt;&lt;br&gt;    &lt;span style="font-style:italic;"&gt;// .NET 2.0 style with anonymous delegate&lt;/span&gt;&lt;br&gt;    SpecHelpers&lt;span&gt;.&lt;/span&gt;ExpectExceptionMessageContains&lt;span&gt;&amp;lt;&lt;/span&gt;NullReferenceException&lt;span&gt;&amp;gt;(&lt;/span&gt;&lt;span style="font-weight:bold;"&gt;delegate&lt;/span&gt;&lt;span&gt;() {&lt;/span&gt; &lt;span&gt;ThrowExContains&lt;/span&gt;&lt;span&gt;(); },&lt;/span&gt; &lt;span&gt;"special"&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;br&gt;&lt;span&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;/pre&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a title="Email Syntactic+Sugar+for+Thrown+Exception+Checking+with+Generics" href = "mailto:?body=Thought you might like this: http://blog.alecl.com/archive/2008/10/25/syntactic-sugar-for-thrown-exception-checking-with-generics.aspx&amp;subject=Syntactic+Sugar+for+Thrown+Exception+Checking+with+Generics"&gt;Email it!&lt;/a&gt; | &lt;a href = "http://del.icio.us/post?url=http://blog.alecl.com/archive/2008/10/25/syntactic-sugar-for-thrown-exception-checking-with-generics.aspx&amp;title=Syntactic+Sugar+for+Thrown+Exception+Checking+with+Generics" title="Submit Syntactic+Sugar+for+Thrown+Exception+Checking+with+Generics to del.icio.us" &gt;bookmark it!&lt;/a&gt; | &lt;a href = "http://www.digg.com/submit?url=http://blog.alecl.com/archive/2008/10/25/syntactic-sugar-for-thrown-exception-checking-with-generics.aspx&amp;phase=2" title="Submit Syntactic+Sugar+for+Thrown+Exception+Checking+with+Generics to digg.com"&gt;digg it!&lt;/a&gt; | &lt;a href = "http://reddit.com/submit?url=http://blog.alecl.com/archive/2008/10/25/syntactic-sugar-for-thrown-exception-checking-with-generics.aspx&amp;title=Syntactic+Sugar+for+Thrown+Exception+Checking+with+Generics" title="Submit Syntactic+Sugar+for+Thrown+Exception+Checking+with+Generics to reddit.com"&gt;reddit!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.alecl.com/aggbug.aspx?PostID=31" width="1" height="1"&gt;</description><category domain="http://blog.alecl.com/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blog.alecl.com/archive/tags/Functional+Programming/default.aspx">Functional Programming</category></item><item><title>Functional Programming and Unit Testing</title><link>http://blog.alecl.com/archive/2008/09/13/functional-programming-and-unit-testing.aspx</link><pubDate>Sat, 13 Sep 2008 17:21:00 GMT</pubDate><guid isPermaLink="false">022ae8cf-15ca-4180-8062-b150ad1660bf:30</guid><dc:creator>alecl</dc:creator><slash:comments>0</slash:comments><comments>http://blog.alecl.com/comments/30.aspx</comments><wfw:commentRss>http://blog.alecl.com/commentrss.aspx?PostID=30</wfw:commentRss><description>These code samples are from:&lt;br&gt;&lt;br&gt;&lt;a href="http://www.ayende.com/Blog/archive/2008/06/06/Scratching-an-itch-NMemcached.aspx" target="_blank"&gt;http://www.ayende.com/Blog/archive/2008/06/06/Scratching-an-itch-NMemcached.aspx&lt;/a&gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public class SystemTime&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;{&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Func&amp;lt;DateTime&amp;gt; Now = () =&amp;gt; DateTime.Now;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;br&gt;&lt;br&gt;At first I was wondering what the point of that was.&amp;nbsp; Does he really hate the name DateTime?&amp;nbsp; Was he planning on changing the class one day to not use DateTime.Now and wanted a layer of indirection?&amp;nbsp; Turns out that was on the right track, but not exactly it.&lt;br&gt;&lt;br&gt;&lt;a href="https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/experiments/NMemcached/NMemcached.Tests/Memcache_FlushAll_Tests.cs" target="_blank"&gt;https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/experiments/NMemcached/NMemcached.Tests/Memcache_FlushAll_Tests.cs&lt;/a&gt;&lt;br&gt;&lt;br&gt;After seeing his unit tests I figured it out.&amp;nbsp; He changes the value of the static function SystemTime.Now from being a function call that is evaluated each time to returning a burned in value of January 1, 2000.&amp;nbsp; Now his code that checks for the current time to see if something is expired will see that date.&amp;nbsp; He tests this in "When_getting_item_that_has_been_expired_will_return_empty_result()".&amp;nbsp; You could do something messier like adding a cache item that is set to expire in 2 seconds, sleeping 2 seconds, and then checking that it's gone, modifying your classes to allow passing in a DateTime or setting it via reflection, but this is so much neater and doesn't introduce delays into your unit tests which should run as fast as possible.&lt;br&gt;&lt;br&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a title="Email Functional+Programming+and+Unit+Testing" href = "mailto:?body=Thought you might like this: http://blog.alecl.com/archive/2008/09/13/functional-programming-and-unit-testing.aspx&amp;subject=Functional+Programming+and+Unit+Testing"&gt;Email it!&lt;/a&gt; | &lt;a href = "http://del.icio.us/post?url=http://blog.alecl.com/archive/2008/09/13/functional-programming-and-unit-testing.aspx&amp;title=Functional+Programming+and+Unit+Testing" title="Submit Functional+Programming+and+Unit+Testing to del.icio.us" &gt;bookmark it!&lt;/a&gt; | &lt;a href = "http://www.digg.com/submit?url=http://blog.alecl.com/archive/2008/09/13/functional-programming-and-unit-testing.aspx&amp;phase=2" title="Submit Functional+Programming+and+Unit+Testing to digg.com"&gt;digg it!&lt;/a&gt; | &lt;a href = "http://reddit.com/submit?url=http://blog.alecl.com/archive/2008/09/13/functional-programming-and-unit-testing.aspx&amp;title=Functional+Programming+and+Unit+Testing" title="Submit Functional+Programming+and+Unit+Testing to reddit.com"&gt;reddit!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.alecl.com/aggbug.aspx?PostID=30" width="1" height="1"&gt;</description><category domain="http://blog.alecl.com/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blog.alecl.com/archive/tags/Functional+Programming/default.aspx">Functional Programming</category></item><item><title>Good Overview of Types of Testing and TDD</title><link>http://blog.alecl.com/archive/2008/02/22/good-overview-of-types-of-testing-and-tdd.aspx</link><pubDate>Sat, 23 Feb 2008 00:58:00 GMT</pubDate><guid isPermaLink="false">022ae8cf-15ca-4180-8062-b150ad1660bf:28</guid><dc:creator>alecl</dc:creator><slash:comments>0</slash:comments><comments>http://blog.alecl.com/comments/28.aspx</comments><wfw:commentRss>http://blog.alecl.com/commentrss.aspx?PostID=28</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://www.leonmeijer.nl/archive/2007/06/05/45.aspx" id="_1ab251cd220f_HomePageDays_DaysList_ctl14_DayItem_DayList_ctl00_TitleUrl" title="Click To View Entry."&gt;Test-driven development, Unit Test, VSTS, NUnit, TestDriven.NET, whats all this?&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Good Overview of Types of Testing and TDD&amp;nbsp;&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a title="Email Good+Overview+of+Types+of+Testing+and+TDD" href = "mailto:?body=Thought you might like this: http://blog.alecl.com/archive/2008/02/22/good-overview-of-types-of-testing-and-tdd.aspx&amp;subject=Good+Overview+of+Types+of+Testing+and+TDD"&gt;Email it!&lt;/a&gt; | &lt;a href = "http://del.icio.us/post?url=http://blog.alecl.com/archive/2008/02/22/good-overview-of-types-of-testing-and-tdd.aspx&amp;title=Good+Overview+of+Types+of+Testing+and+TDD" title="Submit Good+Overview+of+Types+of+Testing+and+TDD to del.icio.us" &gt;bookmark it!&lt;/a&gt; | &lt;a href = "http://www.digg.com/submit?url=http://blog.alecl.com/archive/2008/02/22/good-overview-of-types-of-testing-and-tdd.aspx&amp;phase=2" title="Submit Good+Overview+of+Types+of+Testing+and+TDD to digg.com"&gt;digg it!&lt;/a&gt; | &lt;a href = "http://reddit.com/submit?url=http://blog.alecl.com/archive/2008/02/22/good-overview-of-types-of-testing-and-tdd.aspx&amp;title=Good+Overview+of+Types+of+Testing+and+TDD" title="Submit Good+Overview+of+Types+of+Testing+and+TDD to reddit.com"&gt;reddit!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.alecl.com/aggbug.aspx?PostID=28" width="1" height="1"&gt;</description><category domain="http://blog.alecl.com/archive/tags/Unit+Testing/default.aspx">Unit Testing</category></item><item><title>Testing Theories Part 4 - TypeMock Isolator - The Dark Horse of Unit Testing?</title><link>http://blog.alecl.com/archive/2008/02/18/typemock-the-dark-horse-of-unit-testing.aspx</link><pubDate>Tue, 19 Feb 2008 03:17:00 GMT</pubDate><guid isPermaLink="false">022ae8cf-15ca-4180-8062-b150ad1660bf:8</guid><dc:creator>alecl</dc:creator><slash:comments>3</slash:comments><comments>http://blog.alecl.com/comments/8.aspx</comments><wfw:commentRss>http://blog.alecl.com/commentrss.aspx?PostID=8</wfw:commentRss><description>&lt;div&gt;Another in a series of posts that in no small part will serve as
summaries/reminders to myself on various readings on the topic of
automated code testing.&amp;nbsp; Perhaps others will find them useful though.&amp;nbsp;
I can't take credit for any terribly creative conjectures in my
conclusions/summaries.&amp;nbsp; They almost exclusively come from the writers
in the referenced material.&amp;nbsp; I would strongly encourage reading the
original source materials for far more details and a better
understanding of the topics.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;a href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-1-classical-vs-mock-testing.aspx" target="_blank"&gt;Testing Theories Part 1 - Classical vs Mock Testing&lt;/a&gt;&lt;br&gt;&lt;a href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-2-unit-testing-with-inversion-of-control-pro-s-con-s.aspx" id="bp___v___r___postlist___EntryItems_ctl02_PostTitle"&gt;&lt;/a&gt;&lt;a href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-2-unit-testing-with-inversion-of-control-pro-s-con-s.aspx" target="_blank"&gt;
Testing Theories Part 2 - Unit Testing with Inversion of Control Pro's/Con's&lt;/a&gt;&lt;br&gt;
&lt;a href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-3-ioc-containers-castle-windsor-spring-net-automocking-containers.aspx" id="bp___v___r___postlist___EntryItems_ctl01_PostTitle"&gt;Testing Theories Part 3 - IoC Containers (Castle Windsor, Spring.NET), AutoMocking Containers&lt;br&gt;
&lt;/a&gt;Testing Theories Part 4 - TypeMock Isolator - The Dark Horse of Unit Testing? &lt;p&gt;A lot has been discussed on the pro's/con's of IoC for the purposes of unit testing and in general it has been framed as for or against TDD.&amp;nbsp; This is actually not entirely correct.&amp;nbsp; &lt;a href="http://www.typemock.com/" target="_blank"&gt;TypeMock Isolator&lt;/a&gt; has the notion of &lt;a href="http://www.typemock.com/Docs/HowTypeMockHelps.html" target="_blank"&gt;Reflective Mocks&lt;/a&gt; that allow setting up expectations on actual classes and not just interfaces.&amp;nbsp; I've been fascinated with the idea of TypeMock and it introduces the concept that you don't HAVE to modify your design for testability using IoC, interfaces, and factories strictly for testing purposes.&amp;nbsp; At the Philly .NET Code Camp in January the ALT.NET track was very heavily into testability via IoC.&amp;nbsp; Also as part of that track, I attended &lt;a href="http://donxml.com/Default.aspx" target="_blank"&gt;Don's&lt;/a&gt; presentation on design patterns and I had asked him afterwards how he achieved testability in his sample code when he did not implement IoC and the design pattern implementation would have made it difficult to do so.&amp;nbsp; He mentioned using TypeMock, a product I had been reading a lot about, and it definitely made me think of this product more seriously.&lt;/p&gt;&lt;p&gt;This article, &lt;a href="http://www.codeproject.com/KB/dotnet/StopDesign4Tests.aspx" target="_blank"&gt;Stop Designing for Testability&lt;/a&gt; by &lt;a href="http://www.typemock.com/" target="_blank"&gt;TypeMock Isolator&lt;/a&gt; evangelist &lt;a href="http://www.elilopian.com/" target="_blank"&gt;Eli Lopian&lt;/a&gt; also promotes the bold assertion that IoC is not the only path to testability.&amp;nbsp; The article itself is very thought provoking as well as the discussion in the comments.&amp;nbsp; &lt;a href="http://www.codeproject.com/KB/dotnet/StopDesign4Tests.aspx?msg=1976801#xx1976801xx" target="_blank"&gt;One particular comment&lt;/a&gt; by narsyseth (sadavoya) draws a great a parallel to white box and black box testing.&lt;/p&gt;&lt;p&gt;"Hi Guys,&lt;br&gt;This has been an interesting thread to watch. I see this
moving toward a general discussion on how to test. It seems to me that
there are two types of unit testing, white box and black box. Both are
equally useful at different times. Black box unit tests would test an
object using it's interface. The onus is on the production code to
provide the flexibility to inject mocks, but those mocks require
limited knowledge of what they are "mocking". White box tests would
involve mocks that have more intimate knowledge of exactly how the unit
they are "mocking" works. It seems to me that Typemock leans toward the
white box approach, while the use of factories is more black box. I
realize it's not so clear cut, but that's a pattern I'm seeing here.&amp;nbsp; Regardless
of whether or not that's true, where I see something like Typemock
really shining is when writing tests for legacy code, when you have no
tests to begin with. Assuming the legacy code is not written in a
test-friendly way, using the black box approach is extremely difficult:
you can't refactor the legacy code to support mock injection because
without tests you can't be sure you won't break it. On the other hand
you can't inject mocks because the legacy code doesn't support it. Typemock
breaks this vicious circle, it seems, by allowing the injection of
mocks without touching the production code. This way you can write
tests, then safely refactor the production code. Voila, the legacy code
can now be brought into a testing cycle and the 21st century. For me, this is the most exciting aspect of Typemock."&lt;/p&gt;&lt;p&gt;Some detractors mention that since TypeMock will let you test nearly anything, you'll have no incentive to adhere to any of the solid principles that come along with testability via IoC such as designing to interfaces, isolation, lower coupling, and single responsibility.&amp;nbsp; That simply doesn't seem to be a clear argument.&amp;nbsp; Having a more flexible tool doesn't automatically make you a sloppier programmer.&amp;nbsp; Ideally your own discipline should motivate you, not the limitations of a tool.&lt;br&gt;&lt;/p&gt;
&lt;div&gt;&lt;span class="625004717-18022008"&gt;&lt;font face="Arial" size="2"&gt;It is still possible to use TypeMock just as a regular interface based mock tool using natural mocks.&amp;nbsp; &lt;a href="http://www.paraesthesia.com/archive/2008/01/14/designing-for-testability-with-typemock.aspx" target="_blank"&gt;The reflective features aren't forced on you&lt;/a&gt;.&lt;/font&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span class="625004717-18022008"&gt;&lt;/span&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;span class="625004717-18022008"&gt;&lt;font face="Arial" size="2"&gt;&lt;a href="http://weblogs.asp.net/rosherove/archive/2008/01/17/is-typemock-too-powerful-will-it-kill-design-for-testability.aspx" title="http://weblogs.asp.net/rosherove/archive/2008/01/17/is-typemock-too-powerful-will-it-kill-design-for-testability.aspx"&gt;Roy Osherove&lt;/a&gt;, formerly on the fence about the power of TypeMock and now affiliated with it, also provides some insights on why TypeMock shouldn't be avoided just because it's flexible.&lt;/font&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;Personally, while I have done research and information gathering from various online sources and in-person conferences, there is no substitute for sufficient hands on experience with Unit Testing and TDD.&amp;nbsp; I'm still new enough to it to have a &lt;a href="http://blog.alecl.com/archive/2008/01/14/assigning-responsibilities.aspx" target="_blank"&gt;revelation&lt;/a&gt; that come about only when I was trying to retrofit testing into legacy code.&amp;nbsp; Had I been using TypeMock, I very well may have just used Reflective Mocking and not realized that my design wasn't very well layered nor were my classes following single responsibility very well.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;I do have some reservations about instituting a non-free component into our codebase.&amp;nbsp; The cost is pretty reasonable.&amp;nbsp; The main concern is that the community usage is less than free alternatives like Rhino Mocks so you're more likely to run into a blogger or another developer at a user group and discuss Rhino Mocks than TypeMock.&amp;nbsp; The basic principles of mocking seem fairly similar though, especially with the Natural Mock support so I'm willing to give it a shot.&amp;nbsp; There is a downside to the latest open source trends as well.&amp;nbsp; They can be quickly replaced by the next big free thing.&lt;br&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;For now I will be doing IoC and using non-reflective Mocks through both TypeMock and Rhino Mocks for a few features while I'm still getting the hand of it to get a good frame of reference on both.&amp;nbsp; For legacy code that I am simply adding tests to because I'm not making significant feature changes (can't justify the full refactor and QA cycle of a rewrite), I'll use reflective mocks from Type Mock.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;After I become more experienced in making testability design decisions, I'll consider which framework to use for new code as well.&lt;/div&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a title="Email Testing+Theories+Part+4+-+TypeMock+Isolator+-+The+Dark+Horse+of+Unit+Testing%3f" href = "mailto:?body=Thought you might like this: http://blog.alecl.com/archive/2008/02/18/typemock-the-dark-horse-of-unit-testing.aspx&amp;subject=Testing+Theories+Part+4+-+TypeMock+Isolator+-+The+Dark+Horse+of+Unit+Testing%3f"&gt;Email it!&lt;/a&gt; | &lt;a href = "http://del.icio.us/post?url=http://blog.alecl.com/archive/2008/02/18/typemock-the-dark-horse-of-unit-testing.aspx&amp;title=Testing+Theories+Part+4+-+TypeMock+Isolator+-+The+Dark+Horse+of+Unit+Testing%3f" title="Submit Testing+Theories+Part+4+-+TypeMock+Isolator+-+The+Dark+Horse+of+Unit+Testing%3f to del.icio.us" &gt;bookmark it!&lt;/a&gt; | &lt;a href = "http://www.digg.com/submit?url=http://blog.alecl.com/archive/2008/02/18/typemock-the-dark-horse-of-unit-testing.aspx&amp;phase=2" title="Submit Testing+Theories+Part+4+-+TypeMock+Isolator+-+The+Dark+Horse+of+Unit+Testing%3f to digg.com"&gt;digg it!&lt;/a&gt; | &lt;a href = "http://reddit.com/submit?url=http://blog.alecl.com/archive/2008/02/18/typemock-the-dark-horse-of-unit-testing.aspx&amp;title=Testing+Theories+Part+4+-+TypeMock+Isolator+-+The+Dark+Horse+of+Unit+Testing%3f" title="Submit Testing+Theories+Part+4+-+TypeMock+Isolator+-+The+Dark+Horse+of+Unit+Testing%3f to reddit.com"&gt;reddit!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.alecl.com/aggbug.aspx?PostID=8" width="1" height="1"&gt;</description><category domain="http://blog.alecl.com/archive/tags/Unit+Testing/default.aspx">Unit Testing</category></item><item><title>Testing Theories Part 3 - IoC Containers (Castle Windsor, Spring.NET), AutoMocking Containers</title><link>http://blog.alecl.com/archive/2008/02/18/testing-theories-part-3-ioc-containers-castle-windsor-spring-net-automocking-containers.aspx</link><pubDate>Tue, 19 Feb 2008 00:41:00 GMT</pubDate><guid isPermaLink="false">022ae8cf-15ca-4180-8062-b150ad1660bf:12</guid><dc:creator>alecl</dc:creator><slash:comments>4</slash:comments><comments>http://blog.alecl.com/comments/12.aspx</comments><wfw:commentRss>http://blog.alecl.com/commentrss.aspx?PostID=12</wfw:commentRss><description>&lt;div&gt;Another in a series of posts that in no small part will serve as
summaries/reminders to myself on various readings on the topic of
automated code testing.&amp;nbsp; Perhaps others will find them useful though.&amp;nbsp;
I can't take credit for any terribly creative conjectures in my
conclusions/summaries.&amp;nbsp; They almost exclusively come from the writers
in the referenced material.&amp;nbsp; I would strongly encourage reading the
original source materials for far more details and a better
understanding of the topics.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;a href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-1-classical-vs-mock-testing.aspx" target="_blank"&gt;Testing Theories Part 1 - Classical vs Mock Testing&lt;/a&gt;&lt;br&gt;

&lt;a href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-2-unit-testing-with-inversion-of-control-pro-s-con-s.aspx" id="bp___v___r___postlist___EntryItems_ctl02_PostTitle"&gt;Testing Theories Part 2 - Unit Testing with Inversion of Control Pro's/Con's&lt;br&gt;
&lt;/a&gt;Testing Theories Part 3 - IoC Containers (Castle Windsor, Spring.NET), AutoMocking Containers&lt;a href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-3-ioc-containers-castle-windsor-spring-net-automocking-containers.aspx" id="bp___v___r___postlist___EntryItems_ctl01_PostTitle"&gt;&lt;br&gt;
&lt;/a&gt;&lt;a href="http://blog.alecl.com/archive/2008/02/18/typemock-the-dark-horse-of-unit-testing.aspx" id="bp___v___r___postlist___EntryItems_ctl00_PostTitle"&gt;Testing Theories Part 4 - TypeMock Isolator - The Dark Horse of Unit Testing?&lt;/a&gt; &lt;p&gt;IoC can certainly be done manually without the help of a container, but there are advantages to using one.&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Can configure external dependencies in config for class so each time it's used the dependencies are automatically wired up.&amp;nbsp; No setting properties or passing lots of interface reference parameters each time.&lt;/li&gt;&lt;li&gt;Can you separate config for unit test, pre-production environment staging, and production so no code changes for where your dependencies/resources come from.&amp;nbsp; Integration tests on a test environment can use a local copy of the DB or completely mock the data layer altogether.&amp;nbsp; Code based config right in the unit tests is also a possibility.&lt;/li&gt;&lt;li&gt;The container saves you the trouble of writing specialized factory classes to configure your objects for various roles.&lt;br&gt;&lt;/li&gt;&lt;li&gt;Dependency lists serve as a form of implicit documentation for your classes and how they are connected.&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;AutoMockingContainers automatically create dynamic mocks for all dependency references needed for the class in test and the specific portion of functionality being tested can then be setup with expectations.&amp;nbsp; It cleans out the setup part of your tests a bit and cuts down on repetition.&lt;br&gt;&lt;/p&gt;&lt;p&gt;JP Boodhoo and David provide similar BDD base class implementations using Windsor AutoMocking container to further clean up redundant test code.&lt;br&gt;&lt;/p&gt;&lt;p&gt;---&lt;/p&gt;&lt;p&gt;&lt;b&gt;Resources&lt;br&gt;&lt;br&gt;Castle Windsor&lt;/b&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;font&gt;&lt;a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx"&gt;http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx&lt;/a&gt;&lt;br&gt;&lt;a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart2.aspx"&gt;http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart2.aspx&lt;/a&gt;&lt;br&gt;&lt;a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart3.aspx"&gt;http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart3.aspx&lt;/a&gt;&lt;br&gt;&lt;a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart4.aspx"&gt;http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart4.aspx&lt;/a&gt;&lt;br&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/aa973811.aspx"&gt;http://msdn2.microsoft.com/en-us/library/aa973811.aspx&lt;/a&gt;&lt;/font&gt; &lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Windsor AutoMockingContainer&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;font&gt;&lt;a href="http://blog.eleutian.com/2007/02/23/TestsAutoMockingIoCContainer.aspx" title="http://blog.eleutian.com/2007/02/23/TestsAutoMockingIoCContainer.aspx"&gt;http://blog.eleutian.com/2007/02/23/TestsAutoMockingIoCContainer.aspx&lt;/a&gt;&lt;br&gt;&lt;a href="http://blog.eleutian.com/2007/08/05/UsingTheAutoMockingContainer.aspx" title="http://blog.eleutian.com/2007/08/05/UsingTheAutoMockingContainer.aspx"&gt;http://blog.eleutian.com/2007/08/05/UsingTheAutoMockingContainer.aspx&lt;/a&gt;&lt;br&gt;&lt;a href="http://www.ayende.com/Blog/archive/2007/06/08/The-Auto-Mocking-Container.aspx" title="http://www.ayende.com/Blog/archive/2007/06/08/The-Auto-Mocking-Container.aspx"&gt;http://www.ayende.com/Blog/archive/2007/06/08/The-Auto-Mocking-Container.aspx&lt;/a&gt;&lt;/font&gt; &lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Spring.NET&lt;/b&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;a href="http://www.developer.com/net/csharp/article.php/10918_3722931_1" target="_blank"&gt;http://www.developer.com/net/csharp/article.php/10918_3722931_1&lt;/a&gt;&lt;br&gt;&lt;a href="http://forum.springframework.net/showthread.php?t=4201" target="_blank"&gt;http://forum.springframework.net/showthread.php?t=4201&lt;/a&gt; - AutoMock Spring&lt;br&gt;&lt;font face="Arial" size="2"&gt;&lt;a href="http://www.ibm.com/developerworks/library/j-mocktest.html" title="http://www.ibm.com/developerworks/library/j-mocktest.html"&gt;http://www.ibm.com/developerworks/library/j-mocktest.html&lt;/a&gt; - Java example but still useful for reference&lt;/font&gt;&lt;br&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;Helper Base Classes for TDD/BDD&lt;br&gt;&lt;a href="http://codebetter.com/blogs/david_laribee/archive/2007/12/17/approaching-bdd.aspx" target="_blank"&gt;http://codebetter.com/blogs/david_laribee/archive/2007/12/17/approaching-bdd.aspx&lt;/a&gt;&lt;br&gt;&lt;a href="http://codebetter.com/blogs/jean-paul_boodhoo/archive/2007/12/19/bdd-specification-base-class.aspx" target="_blank"&gt;http://codebetter.com/blogs/jean-paul_boodhoo/archive/2007/12/19/bdd-specification-base-class.aspx&lt;/a&gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a title="Email Testing+Theories+Part+3+-+IoC+Containers+(Castle+Windsor%2c+Spring.NET)%2c+AutoMocking+Containers" href = "mailto:?body=Thought you might like this: http://blog.alecl.com/archive/2008/02/18/testing-theories-part-3-ioc-containers-castle-windsor-spring-net-automocking-containers.aspx&amp;subject=Testing+Theories+Part+3+-+IoC+Containers+(Castle+Windsor%2c+Spring.NET)%2c+AutoMocking+Containers"&gt;Email it!&lt;/a&gt; | &lt;a href = "http://del.icio.us/post?url=http://blog.alecl.com/archive/2008/02/18/testing-theories-part-3-ioc-containers-castle-windsor-spring-net-automocking-containers.aspx&amp;title=Testing+Theories+Part+3+-+IoC+Containers+(Castle+Windsor%2c+Spring.NET)%2c+AutoMocking+Containers" title="Submit Testing+Theories+Part+3+-+IoC+Containers+(Castle+Windsor%2c+Spring.NET)%2c+AutoMocking+Containers to del.icio.us" &gt;bookmark it!&lt;/a&gt; | &lt;a href = "http://www.digg.com/submit?url=http://blog.alecl.com/archive/2008/02/18/testing-theories-part-3-ioc-containers-castle-windsor-spring-net-automocking-containers.aspx&amp;phase=2" title="Submit Testing+Theories+Part+3+-+IoC+Containers+(Castle+Windsor%2c+Spring.NET)%2c+AutoMocking+Containers to digg.com"&gt;digg it!&lt;/a&gt; | &lt;a href = "http://reddit.com/submit?url=http://blog.alecl.com/archive/2008/02/18/testing-theories-part-3-ioc-containers-castle-windsor-spring-net-automocking-containers.aspx&amp;title=Testing+Theories+Part+3+-+IoC+Containers+(Castle+Windsor%2c+Spring.NET)%2c+AutoMocking+Containers" title="Submit Testing+Theories+Part+3+-+IoC+Containers+(Castle+Windsor%2c+Spring.NET)%2c+AutoMocking+Containers to reddit.com"&gt;reddit!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.alecl.com/aggbug.aspx?PostID=12" width="1" height="1"&gt;</description><category domain="http://blog.alecl.com/archive/tags/Unit+Testing/default.aspx">Unit Testing</category></item><item><title>Testing Theories Part 2 - Unit Testing with Inversion of Control Pro's/Con's</title><link>http://blog.alecl.com/archive/2008/02/18/testing-theories-part-2-unit-testing-with-inversion-of-control-pro-s-con-s.aspx</link><pubDate>Mon, 18 Feb 2008 21:04:00 GMT</pubDate><guid isPermaLink="false">022ae8cf-15ca-4180-8062-b150ad1660bf:10</guid><dc:creator>alecl</dc:creator><slash:comments>4</slash:comments><comments>http://blog.alecl.com/comments/10.aspx</comments><wfw:commentRss>http://blog.alecl.com/commentrss.aspx?PostID=10</wfw:commentRss><description>&lt;DIV&gt;&lt;DIV&gt;Another in a series of posts that in no small part will serve as summaries/reminders to myself on various readings on the topic of automated code testing.  Perhaps others will find them useful though.  I can't take credit for any terribly creative conjectures in my conclusions/summaries.  They almost exclusively come from the writers in the referenced material.  I would strongly encourage reading the original source materials for far more details and a better understanding of the topics.&lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;A href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-1-classical-vs-mock-testing.aspx" target="_blank"&gt;Testing Theories Part 1 - Classical vs Mock Testing&lt;/A&gt;&lt;BR&gt; Testing Theories Part 2 - Unit Testing with Inversion of Control Pro's/Con's&lt;A href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-2-unit-testing-with-inversion-of-control-pro-s-con-s.aspx" id="bp___v___r___postlist___EntryItems_ctl02_PostTitle"&gt;&lt;BR&gt;&lt;/A&gt;&lt;A href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-3-ioc-containers-castle-windsor-spring-net-automocking-containers.aspx" id="bp___v___r___postlist___EntryItems_ctl01_PostTitle"&gt;Testing Theories Part 3 - IoC Containers (Castle Windsor, Spring.NET), AutoMocking Containers&lt;BR&gt;&lt;/A&gt;&lt;A href="http://blog.alecl.com/archive/2008/02/18/typemock-the-dark-horse-of-unit-testing.aspx" id="bp___v___r___postlist___EntryItems_ctl00_PostTitle"&gt;Testing Theories Part 4 - TypeMock Isolator - The Dark Horse of Unit Testing?&lt;/A&gt; &lt;BR&gt;&lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;Inversion of Control (IoC) is often achieved by Dependency Injection and programming to interfaces and can be facilitated using IoC containers such as &lt;A href="http://www.castleproject.org/container/" target="_blank"&gt;Castle Windsor&lt;/A&gt; or &lt;A href="http://www.springframework.net/" target="_blank"&gt;Spring&lt;/A&gt;.&lt;BR&gt;&lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;Pro&lt;/DIV&gt;&lt;DIV&gt;&lt;OL&gt;&lt;LI&gt;Improves modularity and extensibility through &lt;A href="http://en.wikipedia.org/wiki/Interface_based_programming" target="_blank"&gt;interface based programming&lt;/A&gt; - it's easier to add/swap out implementations in the future and mock frameworks are well suited to mocking interfaces&lt;BR&gt;&lt;/LI&gt;&lt;LI&gt;Reduces coupling and dependency through &lt;A href="http://blogs.msdn.com/ploeh/archive/2007/05/30/ReasonsForIsolation.aspx" target="_blank"&gt;isolation and decoupling into layers&lt;/A&gt; - layers can be replaced in the future or if that is not needed, they still gain the benefit of individual development and testing brought about by the reduction in dependencies&lt;BR&gt;&lt;/LI&gt;&lt;LI&gt;&lt;A href="http://en.wikipedia.org/wiki/Single_responsibility_principle" target="_blank"&gt;Promotes Single Responsibility Principle&lt;/A&gt; - easier to prevent unexpected breakage from unrelated functionality in the same class&lt;/LI&gt;&lt;LI&gt;All of the above facilitate creating fine grained unit tests that test only one thing and run quickly, especially using &lt;A href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-1-classical-vs-mock-testing.aspx" target="_blank"&gt;Mocks&lt;/A&gt;.&lt;/LI&gt;&lt;LI&gt;Explictly defining your external dependencies in constructors makes them clear to users of the class serving to improve implicit documentation&lt;BR&gt;&lt;/LI&gt;&lt;/OL&gt;Note: #1-#3 above result from a solid implementation of IoC principles.  Nevertheless, aside from the facets of isolation that result strictly from dependency injection, the rest of the principles mentioned in #1 - #3 above are not specific to inversion of control.  This is an important distinction as often arguments FOR IoC are just arguments for some of those good, base programming principles and IoC is not required for their implementation&lt;BR&gt;&lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;Con&lt;/DIV&gt;&lt;DIV&gt;&lt;UL&gt;&lt;LI&gt;Leads to a lot of classes generated for interfaces and factories on objects that are not required for production code and many objects are perhaps known to be simple and very unlikely to change.  Violates &lt;A href="http://fishbowl.pastiche.org/2004/03/02/defending_yagni" target="_blank"&gt;YAGNI&lt;/A&gt;.&lt;/LI&gt;&lt;LI&gt;Tends to deviate from some facets of &lt;A href="http://en.wikipedia.org/wiki/Information_hiding" target="_blank"&gt;encapsulation&lt;/A&gt; given that dependencies which can arguably be considered to be implementation details are made external and must be known to be passed in/set.&lt;/LI&gt;&lt;LI&gt;Testability via IoC doesn't deal well with static methods or singletons.  While there may be certain good reasons for avoiding these at times, at others they are useful and do serve a purpose, but you have no choice&lt;/LI&gt;&lt;LI&gt;Similarly, while programming to interfaces is often useful and recommended, there are times when inheritance lends itself more easily to the design and IoC becomes kludgy to implement when dealing with a dependency that can consist of any of a inheritance chain of classes. [Need example for this - not as clear in my mind as when I had thought of this a while back]&lt;BR&gt;&lt;/LI&gt;&lt;LI&gt;Difficult to retrofit legacy code to use IoC&lt;BR&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;--- &lt;/DIV&gt;&lt;DIV&gt;Resources &lt;BR&gt;More Debate: http://kohari.org/2007/08/15/defending-dependency-injection/&lt;BR&gt;&lt;/DIV&gt;&lt;P&gt;Pro&lt;/P&gt;&lt;DIV&gt;&lt;FONT face="Arial" size="2"&gt;&lt;A href="http://crazybob.org/2007/06/introduction-to-guice-video-redux.html"&gt;http://msdn2.microsoft.com/en-us/library/aa973811.aspx&lt;BR&gt;http://crazybob.org/2007/06/introduction-to-guice-video-redux.html&lt;/A&gt;&lt;BR&gt;&lt;A href="http://64.233.169.104/search?q=cache:8Mb5G10FvbAJ:kohari.org/2007/08/15/defending-dependency-injection/trackback/+http://kohari.org/2007/08/15/defending-dependency-injection&amp;hl=en&amp;ct=clnk&amp;cd=1&amp;gl=us"&gt;http://64.233.169.104/search?q=cache:8Mb5G10FvbAJ:kohari.org/2007/08/15/defending-dependency-injection/trackback/+http://kohari.org/2007/08/15/defending-dependency-injection&amp;amp;hl=en&amp;amp;ct=clnk&amp;amp;cd=1&amp;amp;gl=us&lt;/A&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx"&gt;http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://blogs.msdn.com/ploeh/archive/2007/05/30/ReasonsForIsolation.aspx"&gt;http://blogs.msdn.com/ploeh/archive/2007/05/30/ReasonsForIsolation.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://www.ayende.com/Blog/archive/2007/08/21/Dependency-Injection-Applicability-Benefits-and-Mocking.aspx"&gt;http://www.ayende.com/Blog/archive/2007/08/21/Dependency-Injection-Applicability-Benefits-and-Mocking.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://ayende.com/Blog/archive/2007/08/18/Dependency-Injection-IAmDonQuixote.aspx"&gt;http://ayende.com/Blog/archive/2007/08/18/Dependency-Injection-IAmDonQuixote.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://www.ayende.com/Blog/archive/2007/08/18/Dependency-Injection-More-than-a-testing-seam.aspx"&gt;http://www.ayende.com/Blog/archive/2007/08/18/Dependency-Injection-More-than-a-testing-seam.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://www.ayende.com/Blog/archive/2007/08/23/Dependency-Injection-in-a-Dynamic-Environment.aspx"&gt;http://www.ayende.com/Blog/archive/2007/08/23/Dependency-Injection-in-a-Dynamic-Environment.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://igloocoder.com/archive/2007/08/21/1286.aspx"&gt;http://igloocoder.com/archive/2007/08/21/1286.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://igloocoder.com/archive/2007/07/25/1277.aspx"&gt;http://igloocoder.com/archive/2007/07/25/1277.aspx&lt;/A&gt;  - refactoring with ndepend as a guide&lt;BR&gt;&lt;A href="http://www.ayende.com/Blog/archive/2006/09/19/7262.aspx"&gt;http://www.ayende.com/Blog/archive/2006/09/19/7262.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://www.netobjectives.com/streamzines/MockObjectsAndMockTurtles/player.html"&gt;http://www.netobjectives.com/streamzines/MockObjectsAndMockTurtles/player.html&lt;/A&gt;&lt;BR&gt;&lt;A href="http://www.ayende.com/Blog/archive/2006/07/02/7436.aspx"&gt;http://www.ayende.com/Blog/archive/2006/07/02/7436.aspx&lt;/A&gt;  - mocking System.Data ADO is a pain!  Easier to ignore the DB altogether&lt;BR&gt;&lt;A href="http://andersnoras.com/blogs/anoras/archive/2007/03/04/balancing-maintainability-readability-and-testability.aspx"&gt;http://andersnoras.com/blogs/anoras/archive/2007/03/04/balancing-maintainability-readability-and-testability.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/rosherove/archive/2007/03/02/testable-designs-round-2-tooling-design-smells-and-bad-analogies.aspx"&gt;http://weblogs.asp.net/rosherove/archive/2007/03/02/testable-designs-round-2-tooling-design-smells-and-bad-analogies.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://ayende.com/Blog/archive/2007/03/04/new-post.aspx"&gt;http://ayende.com/Blog/archive/2007/03/04/new-post.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://ayende.com/Blog/archive/2007/03/05/Great-Code-and-Average-Programmers.aspx"&gt;http://ayende.com/Blog/archive/2007/03/05/Great-Code-and-Average-Programmers.aspx&lt;/A&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;FONT face="Arial" size="2"&gt;Con&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Arial" size="2"&gt;&lt;A href="http://crazybob.org/2007/06/introduction-to-guice-video-redux.html"&gt;http://www.codeproject.com/KB/dotnet/StopDesign4Tests.aspx&lt;/A&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;FONT face="Arial" size="2"&gt;&lt;A href="http://scruffylookingcatherder.com/archive/2007/08/07/dependency-injection.aspx"&gt;http://scruffylookingcatherder.com/archive/2007/08/07/dependency-injection.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://scruffylookingcatherder.com/archive/2007/06/20/mocking-developments.aspx"&gt;http://scruffylookingcatherder.com/archive/2007/06/20/mocking-developments.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://blog.eleutian.com/2007/08/18/DependencyInjectionTypeMockWindmillsAndOtherStuff.aspx"&gt;http://blog.eleutian.com/2007/08/18/DependencyInjectionTypeMockWindmillsAndOtherStuff.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://scruffylookingcatherder.com/archive/2007/08/16/tilting-at-windmills.aspx"&gt;http://scruffylookingcatherder.com/archive/2007/08/16/tilting-at-windmills.aspx&lt;/A&gt;&lt;BR&gt;&lt;A href="http://www.elilopian.com/2007/08/19/dependency-injection-keep-your-privates-to-yourself/"&gt;http://www.elilopian.com/2007/08/19/dependency-injection-keep-your-privates-to-yourself/&lt;/A&gt;&lt;BR&gt;&lt;A href="http://colinjack.blogspot.com/2006/08/typemock-helps-improve-your-designs.html"&gt;http://colinjack.blogspot.com/2006/08/typemock-helps-improve-your-designs.html&lt;/A&gt;&lt;BR&gt;&lt;A href="http://qgyen.net/archive/typemock-is-wonderful/"&gt;http://qgyen.net/archive/typemock-is-wonderful/&lt;/A&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a title="Email Testing+Theories+Part+2+-+Unit+Testing+with+Inversion+of+Control+Pro%27s%2fCon%27s" href = "mailto:?body=Thought you might like this: http://blog.alecl.com/archive/2008/02/18/testing-theories-part-2-unit-testing-with-inversion-of-control-pro-s-con-s.aspx&amp;subject=Testing+Theories+Part+2+-+Unit+Testing+with+Inversion+of+Control+Pro%27s%2fCon%27s"&gt;Email it!&lt;/a&gt; | &lt;a href = "http://del.icio.us/post?url=http://blog.alecl.com/archive/2008/02/18/testing-theories-part-2-unit-testing-with-inversion-of-control-pro-s-con-s.aspx&amp;title=Testing+Theories+Part+2+-+Unit+Testing+with+Inversion+of+Control+Pro%27s%2fCon%27s" title="Submit Testing+Theories+Part+2+-+Unit+Testing+with+Inversion+of+Control+Pro%27s%2fCon%27s to del.icio.us" &gt;bookmark it!&lt;/a&gt; | &lt;a href = "http://www.digg.com/submit?url=http://blog.alecl.com/archive/2008/02/18/testing-theories-part-2-unit-testing-with-inversion-of-control-pro-s-con-s.aspx&amp;phase=2" title="Submit Testing+Theories+Part+2+-+Unit+Testing+with+Inversion+of+Control+Pro%27s%2fCon%27s to digg.com"&gt;digg it!&lt;/a&gt; | &lt;a href = "http://reddit.com/submit?url=http://blog.alecl.com/archive/2008/02/18/testing-theories-part-2-unit-testing-with-inversion-of-control-pro-s-con-s.aspx&amp;title=Testing+Theories+Part+2+-+Unit+Testing+with+Inversion+of+Control+Pro%27s%2fCon%27s" title="Submit Testing+Theories+Part+2+-+Unit+Testing+with+Inversion+of+Control+Pro%27s%2fCon%27s to reddit.com"&gt;reddit!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.alecl.com/aggbug.aspx?PostID=10" width="1" height="1"&gt;</description><category domain="http://blog.alecl.com/archive/tags/Unit+Testing/default.aspx">Unit Testing</category></item><item><title>Testing Theories Part 1 - Classical vs Mock Testing</title><link>http://blog.alecl.com/archive/2008/02/18/testing-theories-part-1-classical-vs-mock-testing.aspx</link><pubDate>Mon, 18 Feb 2008 18:01:00 GMT</pubDate><guid isPermaLink="false">022ae8cf-15ca-4180-8062-b150ad1660bf:9</guid><dc:creator>alecl</dc:creator><slash:comments>3</slash:comments><comments>http://blog.alecl.com/comments/9.aspx</comments><wfw:commentRss>http://blog.alecl.com/commentrss.aspx?PostID=9</wfw:commentRss><description>&lt;div&gt;I'll begin a series of posts that in no small part will serve as summaries/reminders to myself on various readings on the topic of automated code testing.&amp;nbsp; Perhaps others will find them useful though.&amp;nbsp; I can't take credit for any terribly creative conjectures in my conclusions/summaries.&amp;nbsp; They almost exclusively come from the writers in the referenced material.&amp;nbsp; I would strongly encourage reading the original source materials for far more details and a better understanding of the topics.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;Testing Theories Part 1 - Classical vs Mock Testing&lt;br&gt;&lt;a href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-2-unit-testing-with-inversion-of-control-pro-s-con-s.aspx" id="bp___v___r___postlist___EntryItems_ctl02_PostTitle"&gt;Testing Theories Part 2 - Unit Testing with Inversion of Control Pro's/Con's&lt;br&gt;&lt;/a&gt;&lt;a href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-3-ioc-containers-castle-windsor-spring-net-automocking-containers.aspx" id="bp___v___r___postlist___EntryItems_ctl01_PostTitle"&gt;Testing Theories Part 3 - IoC Containers (Castle Windsor, Spring.NET), AutoMocking Containers&lt;br&gt;&lt;/a&gt;&lt;a href="http://blog.alecl.com/archive/2008/02/18/typemock-the-dark-horse-of-unit-testing.aspx" id="bp___v___r___postlist___EntryItems_ctl00_PostTitle"&gt;Testing Theories Part 4 - TypeMock Isolator - The Dark Horse of Unit Testing?&lt;/a&gt;&lt;a href="http://blog.alecl.com/archive/2008/02/18/testing-theories-part-2-unit-testing-with-inversion-of-control-pro-s-con-s.aspx" id="bp___v___r___postlist___EntryItems_ctl02_PostTitle"&gt;&lt;br&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Classical Testing&lt;/b&gt; favors using real objects when possible and stubs/&lt;a href="http://xunitpatterns.com/Test%20Double%20Patterns.html" title="nice examples and reasoning for when various test patterns are best suited" target="_blank"&gt;doubles&lt;/a&gt; when using the real code is not practical due to complex dependencies, slowness, resource availability, etc.&amp;nbsp; In this way, it borders on integration testing more so than unit testing.&amp;nbsp; You are testing more "real code" in each test.&amp;nbsp; Care must be taken to not make tests too coarse grained, or at least also provide fine grained tests to reduce confusion and the time taken to determine the root cause of test failures.&amp;nbsp; There is more of a tendency for coarse grained tests with classical testing than mock testing, but it is up to the diligence of the programmer to do classical testing well.&lt;br&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;In some ways your unit tests are less fragile when refactoring than tests strewn with many mocks which can pepper your tests with implementation details when ideally the tests should know/rely on as little implementation knowledge as possible.&amp;nbsp; This is somewhat reduced by using &lt;a href="http://ayende.com/Wiki/%28S%28tlxfvxvtrzjocj3sf4be0yzj%29%29/Default.aspx?Page=Rhino%20Mocks%20Introduction" target="_blank"&gt;Dynamic Mocks&lt;/a&gt; which only need to set expectations for the code being tested.&amp;nbsp; Conversely, there maybe be some degree of implementation detail needed to make a stub/double so classical testing is not immune from this potential pitfall either.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Mock Testing&lt;/b&gt;, especially using a toolkit such as &lt;a href="http://www.ayende.com/" target="_blank"&gt;Rhino Mocks&lt;/a&gt; or &lt;a href="http://www.typemock.com/" target="_blank"&gt;TypeMock Isolator&lt;/a&gt;, really limits the amount of real code dependencies needed to run a test trending towards finer grained tests.&amp;nbsp; Using an interface (or in the case of TypeMock just the class being tested itself), expectations for method calls, property setting, and return values can be defined and verified.&amp;nbsp; While faking return values can be done quite easily with a stub as well, it would take a fancy stub to be able to verify that a method was simply called and this is trivial and compactly done with mocks.&amp;nbsp; This amount of fine grained control and rigorous testing comes at the price of needing to know and specify the method names and their behavior in the implementation in your test code.&amp;nbsp; Older frameworks relied on mocking using string parameters for class names but type safety has been improved in the latest frameworks and generics support in .NET has helped in this area as well.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;---&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;There is absolutely room for both methodologies even within the same
project.&amp;nbsp; For instance, while it may not be desired as part of your run on every compile test suite, a layer of integration tests in the classical style using as much real code as possible would make an excellent verification that your objects are truly interacting properly in context.&lt;br&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;----&lt;/div&gt;&lt;div&gt;&lt;b&gt;References&lt;/b&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;font face="Arial" size="2"&gt;&lt;a href="http://martinfowler.com/articles/mocksArentStubs.html" target="_blank"&gt;http://martinfowler.com/articles/mocksArentStubs.html&lt;/a&gt;&lt;/font&gt; &lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;font face="Arial" size="2"&gt;&lt;span class="750300215-14012008"&gt;&lt;a href="http://codebetter.com/blogs/ian_cooper/archive/2008/02/04/classicist-vs-mockist-test-driven-development.aspx" target="_blank"&gt;http://codebetter.com/blogs/ian_cooper/archive/2008/02/04/classicist-vs-mockist-test-driven-development.aspx&lt;/a&gt;&lt;/span&gt;&lt;/font&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2005/12/19/135757.aspx" target="_blank"&gt;http://codebetter.com/blogs/jeremy.miller/archive/2005/12/19/135757.aspx&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://weblogs.asp.net/rosherove/archive/2005/06/30/416872.aspx" target="_blank"&gt;http://weblogs.asp.net/rosherove/archive/2005/06/30/416872.aspx&lt;/a&gt;&amp;nbsp;&lt;/div&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a title="Email Testing+Theories+Part+1+-+Classical+vs+Mock+Testing" href = "mailto:?body=Thought you might like this: http://blog.alecl.com/archive/2008/02/18/testing-theories-part-1-classical-vs-mock-testing.aspx&amp;subject=Testing+Theories+Part+1+-+Classical+vs+Mock+Testing"&gt;Email it!&lt;/a&gt; | &lt;a href = "http://del.icio.us/post?url=http://blog.alecl.com/archive/2008/02/18/testing-theories-part-1-classical-vs-mock-testing.aspx&amp;title=Testing+Theories+Part+1+-+Classical+vs+Mock+Testing" title="Submit Testing+Theories+Part+1+-+Classical+vs+Mock+Testing to del.icio.us" &gt;bookmark it!&lt;/a&gt; | &lt;a href = "http://www.digg.com/submit?url=http://blog.alecl.com/archive/2008/02/18/testing-theories-part-1-classical-vs-mock-testing.aspx&amp;phase=2" title="Submit Testing+Theories+Part+1+-+Classical+vs+Mock+Testing to digg.com"&gt;digg it!&lt;/a&gt; | &lt;a href = "http://reddit.com/submit?url=http://blog.alecl.com/archive/2008/02/18/testing-theories-part-1-classical-vs-mock-testing.aspx&amp;title=Testing+Theories+Part+1+-+Classical+vs+Mock+Testing" title="Submit Testing+Theories+Part+1+-+Classical+vs+Mock+Testing to reddit.com"&gt;reddit!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.alecl.com/aggbug.aspx?PostID=9" width="1" height="1"&gt;</description><category domain="http://blog.alecl.com/archive/tags/Unit+Testing/default.aspx">Unit Testing</category></item><item><title>Assigning Responsibilities</title><link>http://blog.alecl.com/archive/2008/01/14/assigning-responsibilities.aspx</link><pubDate>Mon, 14 Jan 2008 15:04:00 GMT</pubDate><guid isPermaLink="false">022ae8cf-15ca-4180-8062-b150ad1660bf:6</guid><dc:creator>alecl</dc:creator><slash:comments>1</slash:comments><comments>http://blog.alecl.com/comments/6.aspx</comments><wfw:commentRss>http://blog.alecl.com/commentrss.aspx?PostID=6</wfw:commentRss><description>&lt;P&gt;&lt;A class="" href="http://codebetter.com/blogs/jeremy.miller/articles/131726.aspx" target=_blank&gt;It’s All about Assigning Responsibilities&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;This example really hit home for me.&amp;nbsp; I am faced with a very similar legacy code design where some processing takes place and an e-mail is sent out.&amp;nbsp; My current&amp;nbsp;tests consist of integration tests that outright send me an e-mail that I have to confirm manually.&amp;nbsp; Having these tests was far better than nothing at all, but their design never sat well with me.&amp;nbsp;&amp;nbsp;They&amp;nbsp;were from an earlier attempt at writing tests before I truly understood or had time to delve into IoC, mocks or refactoring for testability.&lt;/P&gt;
&lt;P&gt;Now&amp;nbsp;half a&amp;nbsp;year&amp;nbsp;after having written the tests, I understood that I could use a mock to determine if an e-mail was sent without sending an actual e-mail.&amp;nbsp; But how would that help me determine if the contents of the e-mail were correct.&amp;nbsp; After reading Jeremy's article it became clear that my code design itself was flawed and I need to simply separate the generation of the e-mail text from sending the e-mail.&amp;nbsp; It's such a simple idea and makes so much sense.&lt;/P&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a title="Email Assigning+Responsibilities" href = "mailto:?body=Thought you might like this: http://blog.alecl.com/archive/2008/01/14/assigning-responsibilities.aspx&amp;subject=Assigning+Responsibilities"&gt;Email it!&lt;/a&gt; | &lt;a href = "http://del.icio.us/post?url=http://blog.alecl.com/archive/2008/01/14/assigning-responsibilities.aspx&amp;title=Assigning+Responsibilities" title="Submit Assigning+Responsibilities to del.icio.us" &gt;bookmark it!&lt;/a&gt; | &lt;a href = "http://www.digg.com/submit?url=http://blog.alecl.com/archive/2008/01/14/assigning-responsibilities.aspx&amp;phase=2" title="Submit Assigning+Responsibilities to digg.com"&gt;digg it!&lt;/a&gt; | &lt;a href = "http://reddit.com/submit?url=http://blog.alecl.com/archive/2008/01/14/assigning-responsibilities.aspx&amp;title=Assigning+Responsibilities" title="Submit Assigning+Responsibilities to reddit.com"&gt;reddit!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.alecl.com/aggbug.aspx?PostID=6" width="1" height="1"&gt;</description><category domain="http://blog.alecl.com/archive/tags/Refactoring/default.aspx">Refactoring</category><category domain="http://blog.alecl.com/archive/tags/Unit+Testing/default.aspx">Unit Testing</category></item><item><title>Promises and Paradigms</title><link>http://blog.alecl.com/archive/2007/02/25/promises-and-paradigms.aspx</link><pubDate>Sun, 25 Feb 2007 22:37:00 GMT</pubDate><guid isPermaLink="false">022ae8cf-15ca-4180-8062-b150ad1660bf:2</guid><dc:creator>alecl</dc:creator><slash:comments>0</slash:comments><comments>http://blog.alecl.com/comments/2.aspx</comments><wfw:commentRss>http://blog.alecl.com/commentrss.aspx?PostID=2</wfw:commentRss><description>&lt;p&gt;I've always tried to make it a point to read a lot about the industry and constantly shifting and improving methods and best practices.&amp;nbsp; Sometimes the buzz dies down after a bit -- I was particularly amused to find people tired of the lofty promises of &lt;a href="http://en.wikipedia.org/wiki/Enterprise_JavaBeans" target="_blank"&gt;EJB&lt;/a&gt; and going back to basics with &lt;a href="http://en.wikipedia.org/wiki/Plain_Old_Java_Object" target="_blank"&gt;POJO&lt;/a&gt;.&amp;nbsp; Other times it grows to a roar.&lt;br&gt;&lt;br&gt;The promises of unit testing and refactoring were being extolled for a while, but I must admit it took some time and much reading for it to sink in.&amp;nbsp; If you're smart enough to make tests to exercise your code cases properly and didn't miss anything, you probably would have handled all those cases properly when you were coding anyway, right?&amp;nbsp; Perhaps, but what about changing that code 6 months from now?&amp;nbsp; What about someone else changing that code?&amp;nbsp; I must admit I find a certain comfortable simplicity in writing unit tests to exercise the boundaries and coverage of a piece of code.&amp;nbsp; After doing a few, it starts to become automatic and not take as much time as you would think.&amp;nbsp; It keeps you honest.&amp;nbsp; Even the best programmers can be burned when making just a quick last minute change that wasn't expected to change another area of code, but invariably does and most people are terrible at methodically manually testing their code countless times.&amp;nbsp; Too many habits of doing things the same way every time leads very often to surprised cries of "But it worked for me.".&amp;nbsp; Unit tests also add a margin of comfort to refactoring.&amp;nbsp; Too often we let bad code persist and create maintenance headaches for years because it's not well suited for automatic or manual testing.&lt;/p&gt;&lt;p&gt;So I'm starting to believe now and writing my unit tests where I can going forward with new code and adding them to existing code when making changes.&amp;nbsp; As time goes on, existing code will be retrofitted for testing and testing code coverage will increase.&amp;nbsp; I'm definitely still learning and experimenting on testing methods and will occasionally post snippets or ideas I'm working on.&lt;br&gt;&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a title="Email Promises+and+Paradigms" href = "mailto:?body=Thought you might like this: http://blog.alecl.com/archive/2007/02/25/promises-and-paradigms.aspx&amp;subject=Promises+and+Paradigms"&gt;Email it!&lt;/a&gt; | &lt;a href = "http://del.icio.us/post?url=http://blog.alecl.com/archive/2007/02/25/promises-and-paradigms.aspx&amp;title=Promises+and+Paradigms" title="Submit Promises+and+Paradigms to del.icio.us" &gt;bookmark it!&lt;/a&gt; | &lt;a href = "http://www.digg.com/submit?url=http://blog.alecl.com/archive/2007/02/25/promises-and-paradigms.aspx&amp;phase=2" title="Submit Promises+and+Paradigms to digg.com"&gt;digg it!&lt;/a&gt; | &lt;a href = "http://reddit.com/submit?url=http://blog.alecl.com/archive/2007/02/25/promises-and-paradigms.aspx&amp;title=Promises+and+Paradigms" title="Submit Promises+and+Paradigms to reddit.com"&gt;reddit!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://blog.alecl.com/aggbug.aspx?PostID=2" width="1" height="1"&gt;</description><category domain="http://blog.alecl.com/archive/tags/Refactoring/default.aspx">Refactoring</category><category domain="http://blog.alecl.com/archive/tags/Unit+Testing/default.aspx">Unit Testing</category></item></channel></rss>