<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>cwash into software &#187; mock objects</title>
	<atom:link href="http://cwash.org/tag/mock-objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://cwash.org</link>
	<description>+= construction + craftsmanship;</description>
	<lastBuildDate>Thu, 29 Sep 2011 04:23:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.2</generator>
		<item>
		<title>Mocking with JMockit</title>
		<link>http://cwash.org/2009/06/09/mocking-with-jmockit/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=mocking-with-jmockit</link>
		<comments>http://cwash.org/2009/06/09/mocking-with-jmockit/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 05:09:58 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Developer Testing]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[CapTech]]></category>
		<category><![CDATA[developer testing]]></category>
		<category><![CDATA[hamcrest]]></category>
		<category><![CDATA[JMockit]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[mock objects]]></category>
		<category><![CDATA[testng]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=322</guid>
		<description><![CDATA[JMockit - overview and example]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2009/06/09/mocking-with-jmockit/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p><strong>Update: </strong> I cleaned up the example based on Rogerio&#8217;s comments.</p>
<p>Recently I stumbled onto <a href="https://jmockit.dev.java.net/">JMockit</a> and have been pretty impressed with the flexibility of the approach it takes.</p>
<p>Many mocking frameworks seem to take an elitist attitude toward testable code, not attempting to solve certain problems in favor of guiding one toward a more testable design.  It appears JMockit is a response to this.<span id="more-322"></span></p>
<p>There&#8217;s no getting around the fact that some frameworks, especially legacy or proprietary third party modules, are not coded in such a way that it&#8217;s easy to write testable code against them.  Common pain points include:</p>
<ul>
<li>Pervasive use of statics</li>
<li>Lack of dependency injection mechanisms</li>
<li>Creating dependencies inline with the &#8220;new&#8221; keyword</li>
</ul>
<p>All of these issues will pose problems when trying to double-out dependent code for testing purposes.  One approach to solve these problems is to use a dynamic language and metaprogramming constructs to do this kind of doubling.  Testing Java code with JRuby or Groovy has become more and more popular for this very reason.</p>
<p>But a lot of these will introduce a level of language abstraction between your test code and the code under test, and you&#8217;ll have to have developers maintaining a test suite sign on to learn the language being used.</p>
<p>In comes JMockit, which uses the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/instrument/Instrumentation.html">instrumentation</a> features provided with Java5 to perform a lot of the same magic tricks for you.</p>
<p>This also allows you to test things that weren&#8217;t otherwise possible (or are very difficult) and overall I think the programming model is much more in-tune with standard Java programming idioms.  It doesn&#8217;t discriminate against you if you don&#8217;t use dependency injection, but will work with you if you do.  Here&#8217;s a quick example that colleagues <a href="http://www.andypemberton.com">Andy Pemberton</a> and Patrick Cox worked through with me trying out JMockit.</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@RunWith<span style="color: #009900;">&#40;</span>JMockit.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ControllerTest <span style="color: #009900;">&#123;</span><br />
&nbsp;<br />
&nbsp; &nbsp; @Mocked<br />
&nbsp; &nbsp; HttpServletRequest mockHttpServletRequest<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; @Mocked<br />
&nbsp; &nbsp; ServiceRemote mockServiceRemote<span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> test_execute_expectations<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">new</span> Expectations<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">/* define in static block */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> Model m <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Model<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.<span style="color: #006633;">setId</span><span style="color: #009900;">&#40;</span>12345l<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mockHttpServletRequest.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;modelId&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> returns<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;12345&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mockServiceRemote.<span style="color: #006633;">getModel</span><span style="color: #009900;">&#40;</span>12345l<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> returns<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mockHttpServletRequest.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;model&quot;</span>, m<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* simulate setter injection */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Controller c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Controller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; c.<span style="color: #006633;">setHttpServletRequest</span><span style="color: #009900;">&#40;</span>mockHttpServletRequest<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; c.<span style="color: #006633;">setServiceRemote</span><span style="color: #009900;">&#40;</span>mockServiceRemote<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* call code under test */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; c.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* strict mode will throw exceptions! */</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>You can get the whole project from my <a href="http://github.com/cwash/testJmockitDemo/tree/master">GitHub account</a>.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2009/06/09/mocking-with-jmockit/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=24&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=74&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=207&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-misterwong">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=6&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to Mister Wong">Add this to Mister Wong</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=4&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-reddit">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=40&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=&amp;service=10&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Mocking+with+JMockit&amp;link=http://cwash.org/2009/06/09/mocking-with-jmockit/&amp;notes=JMockit%20-%20overview%20and%20example&amp;short_link=http://bit.ly/bNQ57B&amp;v=1&amp;apitype=1&amp;apikey=&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul><div style="clear: both;"></div></div>

<h3  class="related_post_title">More Related Content</h3><ul class="related_post"><li>February 17, 2009 -- <a href="http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/" title="Don&#8217;t Unit Test Anymore&#8230; No, Really!">Don&#8217;t Unit Test Anymore&#8230; No, Really!</a> (6)</li><li>June 3, 2009 -- <a href="http://cwash.org/2009/06/03/what-is-hamcrest/" title="What is Hamcrest?">What is Hamcrest?</a> (0)</li><li>January 31, 2009 -- <a href="http://cwash.org/2009/01/31/in-response-to-stackoverflow-38quality-doesnt-matter-that-much-jeff-and-joel/" title="In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel ">In response to Stackoverflow #38/&#8221;Quality Doesn&#8217;t Matter That Much&#8221; &#8212; Jeff and Joel </a> (3)</li><li>November 28, 2008 -- <a href="http://cwash.org/2008/11/28/must-havesreferences-for-modern-java-ee-developers/" title="Must Haves/References For Modern Java EE Developers">Must Haves/References For Modern Java EE Developers</a> (1)</li><li>July 8, 2010 -- <a href="http://cwash.org/2010/07/08/transactions-part-1/" title="Transactions, Part 1">Transactions, Part 1</a> (0)</li><li>January 7, 2010 -- <a href="http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/" title="Eliminate Branching (IF Statements) to Produce Better Code">Eliminate Branching (IF Statements) to Produce Better Code</a> (0)</li><li>July 29, 2009 -- <a href="http://cwash.org/2009/07/29/what-id-tell-myself-about-design-if-i-were-just-beginning/" title="What I&#8217;d Tell Myself About Design If I Were Just Beginning">What I&#8217;d Tell Myself About Design If I Were Just Beginning</a> (5)</li><li>July 24, 2009 -- <a href="http://cwash.org/2009/07/24/the-elements-of-reusable-code/" title="The Elements of Reusable Code">The Elements of Reusable Code</a> (0)</li><li>April 15, 2009 -- <a href="http://cwash.org/2009/04/15/osgi-ggity-giggity/" title="OSGi-ggity-Giggity">OSGi-ggity-Giggity</a> (4)</li><li>November 19, 2008 -- <a href="http://cwash.org/2008/11/19/java-6-and-maven-209-on-leopard/" title="Java 6 and Maven 2.0.9 on Leopard">Java 6 and Maven 2.0.9 on Leopard</a> (7)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2009/06/09/mocking-with-jmockit/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

