<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>8th Light Blog: Over Mocking</title>
    <link>http://blog.8thlight.com/articles/2006/06/09/over-mocking</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In the minds of the craftsmen...</description>
    <item>
      <title>Over Mocking</title>
      <description>&lt;p&gt;I have noticed using more mocks lately.  Instead of using them sparingly for controlling services which are not instrumental to what I am testing, I am essentially using them as a way to isolated what I am testing.  This has me noticing a side effect I am not entirely comfortable with.  To use the mock, I need to be able to have the mock in the production code, which means I need to inject an abstraction of the mock (dynamic mocks will do that for you).  &lt;/p&gt;

&lt;p&gt;So, the side effect is long parameter lists, specifically in constructors.  Everything takes almost all the objects it uses.  Then, to create the production objects are factories.  I am using many more factories than I would without mass mocking.  I am not sure if this is a good use of dependency decoupling and factory building as design techniques, or if this is a symptom of over design and tightly coupling the mocks to the tests, making it more difficult to change the tests without changing the interface the mock is implementing.  Thoughts?&lt;/p&gt;

&lt;p&gt;Wed, 24 May 2006 14:03:11, Dave Hoover, Create a default constructor
Try poor man&amp;#8217;s dependency injection:  have a constructor that allows you to inject your dependencies (hidden behind interfaces) for testing, then have a default constructor that instantiates the concrete objects and injects them itself.&lt;/p&gt;

&lt;p&gt;Wed, 24 May 2006 15:01:02, David Chelimsky, too many dependencies either way
I don&amp;#8217;t think mocks or factories are the real problem here. If A depends on the services of B,C,D,E,F,G,H and I, then it&amp;#8217;s depending on too many things whether you inject the objects or instantiate them internally.&lt;/p&gt;

&lt;p&gt;Wed, 24 May 2006 16:57:06, , 
Don&amp;#8217;t &amp;#8220;inject&amp;#8221; dependencies.  Mock an object&amp;#8217;s peers in tests and pass real peers to it&amp;#8217;s constructor or as method arguments.  Composite objects should create objects they use internally and not expose them at the constructor.  A composite object should be simpler than the sum of its parts.  Also, avoid passing all dependencies into an object&amp;#8217;s constructor: you can probably pass collaborators as method arguments, not constructor arguments.&lt;/p&gt;

&lt;p&gt;Wed, 24 May 2006 17:24:56, Dave Hoover, Listen to Chelimsky
The smell of a long parameter list leads me to think you may be able to collapse some objects behaind a coarser-grained interface.&lt;/p&gt;

&lt;p&gt;Thu, 25 May 2006 11:24:12, Ryan Cooper, DI Containers
I find using a DI container like PicoContainer eases the pain by making all (or most of) those factories unnecessary.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m still not happy about the long parameter lists in constructors, so I think I will give a few of the suggestions above a try. Thanks all.&lt;/p&gt;

&lt;p&gt;Thu, 25 May 2006 16:08:05, Tim Ottinger, The &amp;#8220;Airplane&amp;#8221; retort
&amp;#8220;I&amp;#8217;ll never be over mocking!&amp;#8221;&lt;/p&gt;

&lt;p&gt;Fri, 26 May 2006 10:48:42, Asher Sterkin, Over Mocking
Personally I mock whatever and whenever I can. Usually a hardship with mocking reveals some interesting deeper design problems. If there are too many arguments for constructor it probably means the class under test has too many dependencies, which means a problem with cohesion. In my experience I seldom need to mock more than 5 interfaces per test case. It&amp;#8217;s usually one or two as constructor arguments and two, max three for returned objects. If it&amp;#8217;s more I try to re-factor.&lt;/p&gt;

&lt;p&gt;Fri, 26 May 2006 16:25:43, Paul Pagel, 
&amp;#8220;In my experience I seldom need to mock more than 5 interfaces per test case.&amp;#8221;
Do you only mock interfaces?&lt;/p&gt;

&lt;p&gt;Sat, 27 May 2006 19:49:22, Rogerio Liesenfeld, I prefer to obtain dependencies with &amp;#8220;new&amp;#8221; wherever possible
In the large (8000+ classes) J2EE web app I am currently working, many business classes obtain instances of other business classes by simply instantiating them with the &amp;#8220;new&amp;#8221; operator. Besides being the simplest way to obtain dependencies, this allows business service classes to maintain client-specific state, so that private methods don&amp;#8217;t need to have any parameters.&lt;/p&gt;

&lt;p&gt;Of course, with the mock objects tools previously available, it would have been difficult to unit test such classes. But I wanted to use &amp;#8220;new&amp;#8221;, and also have final classes, final methods, and static methods where appropriate.
So, with that &amp;#8220;itch to scratch&amp;#8221;, I went ahead and created a new tool that would free me to design the application without constraints, and still have complete unit tests.&lt;/p&gt;

&lt;p&gt;If anyone wants to give it a try, the tool is here: jmockit.dev.java.net.&lt;/p&gt;

&lt;p&gt;Fri, 9 Jun 2006 10:30:20, Asher Sterkin, Over Mocking
&amp;#8220;Do you only mock inerfaces?&amp;#8221; Not only, but I prefer so. In C++ it really doesn&amp;#8217;t matter since there is no good mock library (I use my own which is based on link time substitution; I plan to sourceforge it soon). In Java and C# interfaces do not cost too much, but sometimes you have a class comming from the infrastrucure. You would like to mock it but you do not want to wrap it with an extra interface. For example I recently mocked Java&amp;#8217;s TimerTask::cancel in order to validate that in a certain scenario a timer cancellation request is sent properly. I also do test what will my program do if the TimerTask::cancel returns false (too late to cancel).&lt;/p&gt;</description>
      <pubDate>Fri, 09 Jun 2006 04:33:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:cc50edf1-2b05-424d-b092-154356ec9f94</guid>
      <author>Paul Pagel</author>
      <link>http://blog.8thlight.com/articles/2006/06/09/over-mocking</link>
      <category>Testing</category>
      <category>Paul</category>
    </item>
  </channel>
</rss>
