<?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; Developer Testing</title>
	<atom:link href="http://cwash.org/category/software-engineering/developer-testing-software-engineering/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>Eliminate Branching (IF Statements) to Produce Better Code</title>
		<link>http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=eliminate-branching-if-statements-to-produce-better-code</link>
		<comments>http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 21:05:46 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Developer Testing]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[CapTech]]></category>
		<category><![CDATA[exercises]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[SOLID]]></category>
		<category><![CDATA[techniques]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=426</guid>
		<description><![CDATA[In a recent tech talk, I watched Miško Hevery propose an interesting challenge to his audience: start a toy project and try to write the code with no if-else or switch blocks at all. None at all?  Before thinking about how to do this, why would you want to do it in the first place?  On the surface, it may seem to the unassuming a bit counter-intuitive.  Comparison-based branching is at the heart of programming, and the concept of an if-else or switch block is almost universal, existing in most every programming language ever devised.  What's Hevery's agenda with this exercise – and what can possibly be gained from trying to write a program without branching entirely?]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>In a <a title="The Clean Code Talks -- Inheritance, Polymorphism, &amp; Testing" href="http://www.youtube.com/watch?v=4F72VULWFvc&amp;feature=channel">recent tech talk</a>, I watched Miško Hevery propose an interesting challenge to his audience: start a toy project and try to write the code with no if-else or switch blocks at all. None <em>at all</em>?  Before thinking about how to do this, <em>why</em> would you want to do it in the first place?  On the surface, it may seem to the unassuming a bit counter-intuitive.  Comparison-based branching is at the heart of programming, and the concept of an if-else or switch block is almost universal, existing in most every programming language ever devised.  What&#8217;s Hevery&#8217;s agenda with this exercise – and what can possibly be gained from trying to write a program without branching <em>entirely?</em></p>
<p><em><span id="more-426"></span><br />
</em></p>
<h3>Branching Considered Harmful?</h3>
<p>In 1968, <a title="Dijkstra on Wikipedia" href="http://en.wikipedia.org/wiki/Edsger_Dijkstra">E.W. Diskjstra</a>, famed programmer and author of the shortest-path algorithm, wrote an article to be published in the <em>Communications of the ACM</em> entitled &#8220;A Case Against the GOTO Statement&#8221; that was later renamed &#8220;Goto Statement Considered Harmful&#8221; by the editor.  The phrase has grown into a meme of its own in the CS community, denoting a practice that has fallen from grace and become widely discouraged.</p>
<p>I bring this up not to point out its cultural signficance, but the core of his argument:</p>
<blockquote><p><em>The go to statement as it stands is just too primitive[;] it is too much an invitation to make a mess of one&#8217;s program.</em></p></blockquote>
<p>Essentially, Hevery is making this very point for the if-else statement, 40 years later, but for very similar reasons.  If-else and switch statements easily account for some of the ugliest code that I have seen (and written).  Overuse of if-else blocks can lead to the <a title="The Arrow Antipattern on c2wiki" href="http://c2.com/cgi/wiki?ArrowAntiPattern" target="_blank">arrow antipattern</a>, a scenario where conditional branches are so deeply nested they become unmaintainable.  Too often developers fall into this trap, described aptly by another Dijsktra quote:</p>
<blockquote><p><em>The prisoner falls in love with his chains&#8230;</em></p></blockquote>
<p><em><span style="font-style: normal;">Hevery&#8217;s point is that code without if (or switch) statements is:</span></em></p>
<ul>
<li>Easier to read and understand</li>
<li>Easier to test</li>
<li>Easier to maintain, extend, etc.</li>
</ul>
<p>But how do we write code without branching constructs?   If we are talking about fixing the arrow antipattern, you may know a time when you avoided this antipattern by using separation of concerns, for example, the MVC pattern instead of a monolithic servlet.</p>
<h3>Hevery&#8217;s Suggestions</h3>
<p>Hevery goes on to explain how good OO design can eliminate the need to explicitly code the most egregious uses of branching, by instead favoring dependency injection and <a title="Polymorphism on Wikipedia" href="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming">polymorphism</a>.  But this technique is only really useful where we can create methods and objects that are <a title="Cohesion on Wikipedia" href="http://en.wikipedia.org/wiki/Cohesion_(computer_science)#High_cohesion">cohesive</a> and <a title="loose coupling on wikipedia" href="http://en.wikipedia.org/wiki/Loose_coupling" target="_blank">loosely coupled</a>.</p>
<blockquote><p><em>With polymorphism, you dispatch into a method whose binding is not known at compile-time but determined at runtime.</em></p></blockquote>
<p>This is the weapon of choice when we are working on code that is highly procedural and there is a bad OO-design smell.  But it is far from a one-size-fits-all solution.  If we were to transform every branch in our program by instead using polymorphism, it would be a tangle of incohesive objects that would likely be harder to maintain than the original procedural code.  Luckily, there are other techniques that are more appropriate in certain circumstances.</p>
<p>Hevery continues: certainly, primitive comparisons are going to be the most difficult to eliminate, but shouldn&#8217;t happen very often.  So we reluctantly allow these to stick around.  Another class of if statements exist to check against nulls.  Hevery explains how null pointer dereference checks quickly become pandemic, occurring every time a member of an object is referenced.  This should largely be a non-issue, and is only a problem because code is allowing nulls to be returned.  The semantics/method contract of most methods can easily be changed to prohibit the return of nulls.  (This is only really difficult when recursion is used.)  Developers, as it turns out, don&#8217;t have to return null values.  This is particularly true with collections, and warranted a section in Joshua Bloch&#8217;s <a title="Effective Java on Safari" href="http://my.safaribooksonline.com/9780137150021">Effective Java</a>.  Instead return an empty collection can be returned, or you can implement the <a title="Null Object Pattern on Wikipedia" href="http://en.wikipedia.org/wiki/Null_Object_pattern">null object pattern</a> (e.g., a no-op logger).  Hevery finally rallies for ditching using return codes in favor of throwing relevant exceptions.</p>
<p><em>Note: I encourage you to watch the talk, as he continues talking about how specifically you can use inheritance and polymorphism to remove branching. The rest of this article will talk about <strong>other strategies to remove unnecessary branches</strong>.</em></p>
<h3><strong>Remove Copy-Pasted Loops And Conditionals: Introduce Enumerable Operations</strong></h3>
<p><strong> </strong>Many languages contain an enumerable construct that includes &#8220;higher order&#8221; looping operations that can eliminate the need for many specialized cases that are usually solved by a foreach loop with an embedded conditional check of some sort.  This type of construct can be traced back to Smalltalk, and exists in most modern dynamic languages such as Ruby, Groovy, Python, and provided by a number of Javascript libraries.  Getting this to work in Java is a bit of a challenge because there is no mechanism for closures (yet) but some partial solutions do exist (they lack the full power of true closures, but overusing true closures can break down OO-style constructs).  For further information, take a look at <a title="LambdaJ on Google Code" href="http://code.google.com/p/lambdaj/">lambdaj</a>, Adrian Kuhn&#8217;s <a title="Adrian Kuhn: Pimp My Foreach" href="http://www.iam.unibe.ch/~akuhn/blog/2008/pimp-my-foreach-loop/">Pimp My Foreach</a> and <a title="Google Collections Iterators Javadoc" href="http://google-collections.googlecode.com/svn/trunk/javadoc/index.html?com/google/common/collect/Iterators.html">Google Collections&#8217; Iterators</a>.</p>
<p>Here is an example of the types of things this style of programming can fix.</p>
<pre>List&lt;Integer&gt; numbers = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
List&lt;Integer&gt; oddNumbers = new ArrayList&lt;Integer&gt;();

for (Integer check : numbers) {
    if (check % 2 != 0) {
      oddNumbers.add(check);
    }
}</pre>
<p>In the case of the Google Collection, they call their enumberable construct a &#8220;Iterable&#8221; and the closure construct a &#8220;Predicates,&#8221; we&#8217;ll be able to replace the above code with this:</p>
<pre>Predicate&lt;Integer&gt; oddIntegerPredicate = new Predicate&lt;Integer&gt;() {
    public boolean apply(Integer input) {
        return input % 2 != 0;
    }
};

Iterable&lt;Integer&gt; oddNumbers = Iterables.filter(numbers, oddIntegerPredicate);</pre>
<p>LambdaJ, and Kuhn&#8217;s solution work in a similar fashion.  While syntactically not as pretty as the dynamic languages, these constructs are functionally equivalent and arguably no more painful than the original code.  In many cases of deeply nested for-if-for-ifs, it significantly reduces the cyclomatic complexity).  This style of programming, once everyone on the team has become familiar with it, can reduce often copy-and-pasted looping code to one liners, removing some really ugly duplication and the potential for those hard-to-spot copy and paste errors.</p>
<h3>Lookout for Train Wrecks: Obey the Law (of Demeter &#8211; and other SOLID Principles).</h3>
<p>Steve Freeman and Nat Pryce describe &#8220;train wreck&#8221; code in their new book <a title="Growing OO Software Guided by Tests on Safari" href="http://search.safaribooksonline.com/9780321574442">Growing Object Oriented Software Guided By Tests</a> as chained &#8220;getter&#8221; method calls, linked together like train cars.  The authors call them &#8220;wrecks&#8221; because the code is essentially an egregious violation of the Law of Demeter.  One of Nat&#8217;s examples:</p>
<pre>employee.getJobDescription().getResponsibilities().add(new ManagementResponsibility(employee.getDepartment()));</pre>
<p>The problem is that this style of programming destroys all encapsulation and information hiding concepts in code.  Train wrecks can become pandemic as well because instead of using an object-oriented style of programming that passes messages between objects to perform work, we essentially allow objects to know too much about their collaborators and details.  Another way this has been said is &#8220;talk only to your friends&#8221; or &#8220;ask, don&#8217;t tell.&#8221;  A client object <em>asks</em> other objects what their state is, and uses an if statement to perform an action, instead of <em>telling</em> the collaborating object what it wants done, and letting that object figure it out.  Instead of the train wreck, we should extract the behavior to a method that allows us to tell our collaborator what we want done:</p>
<pre>employee.promoteToManager();</pre>
<p>Complex conditional &#8220;train wrecks&#8221; can often be extracted in the same way.  Quite often these types of objects are really just procedural code masquerading as a class, and will be in violation of other SOLID-principles such as the Single Responsibility principle.  Making this code more &#8220;SOLID&#8221; will make it easier to test and maintain.</p>
<h3><strong>The Bigger Picture: Well Designed, Testable Code</strong></h3>
<p>Remember, this exercise in design (by removing ugly branching) is not just for aesthetics.  There is a very legit reason to try these techniques: writing testable code.  Hevery&#8217;s talk (and his other talks) are essentially about how to write code that is testable.  Freeman and Pryce&#8217;s book is about the very close relationship between testability and OO-design.</p>
<p>If you like these ideas, you might be interested to try the <a title="TDD As If You Meant It" href="http://gojko.net/2009/08/02/tdd-as-if-you-meant-it-revisited/">TDD As If You Meant It</a> exercise as well.  This is an exercise that will show you how to listen to test code to drive adding features that are driven out by tests into your production codebase by implementing them first in the test class.  This is the ultimate way to ensure that your production code contains only what it needs and nothing more, and that code is adequately covered by tests!  Remember, the more code you write into production code, the more you&#8217;re going to need to maintain it, and the more skeptical you should be about how readable and well designed it is.  These techniques and exercises may seem silly on the surface, but if you explore them I am confident they will help you write better code!</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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=Eliminate+Branching+%28IF+Statements%29+to+Produce+Better+Code&amp;link=http://cwash.org/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/&amp;notes=In%20a%20recent%20tech%20talk%2C%20I%20watched%20Mi%C5%A1ko%20Hevery%20propose%20an%20interesting%20challenge%20to%20his%20audience%3A%20start%20a%20toy%20project%20and%20try%20to%20write%20the%20code%20with%20no%20if-else%20or%20switch%20blocks%20at%20all.%20None%20at%20all%3F%20%20Before%20thinking%20about%20how%20to%20do%20this%2C%20why%20would%20you%20want%20to%20do%20it%20in%20the%20first%20place%3F%20%20On%20the%20surface%2C%20it%20may%20seem%20to%20the%20unassuming%20a%20bit%20counter-intuitive.%20%20Comparison-based%20branching%20is%20at%20the%20heart%20of%20programming%2C%20and%20the%20concept%20of%20an%20if-else%20or%20switch%20block%20is%20almost%20universal%2C%20existing%20in%20most%20every%20programming%20language%20ever%20devised.%20%20What%27s%20Hevery%27s%20agenda%20with%20this%20exercise%20%E2%80%93%20and%20what%20can%20possibly%20be%20gained%20from%20trying%20to%20write%20a%20program%20without%20branching%20entirely%3F&amp;short_link=http://bit.ly/aGPtFg&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>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>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>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>June 9, 2009 -- <a href="http://cwash.org/2009/06/09/mocking-with-jmockit/" title="Mocking with JMockit">Mocking with JMockit</a> (5)</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>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>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>January 13, 2009 -- <a href="http://cwash.org/2009/01/13/on-software-quality/" title="On Software Quality">On Software Quality</a> (8)</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>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/2010/01/07/eliminate-branching-if-statements-to-produce-better-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>What is Hamcrest?</title>
		<link>http://cwash.org/2009/06/03/what-is-hamcrest/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=what-is-hamcrest</link>
		<comments>http://cwash.org/2009/06/03/what-is-hamcrest/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 10:37:54 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Developer Testing]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[assertThat]]></category>
		<category><![CDATA[CapTech]]></category>
		<category><![CDATA[developer testing]]></category>
		<category><![CDATA[hamcrest]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=220</guid>
		<description><![CDATA[What is Hamcrest?  An introduction to the constraint/matcher/predicate framework for Java.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2009/06/03/what-is-hamcrest/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>As of JUnit 4.4, if you happen to peek into the distributed JAR you&#8217;ll notice something a little off: in addition to the org.junit.* packages there is this a funny-looking org.hamcrest.* package sticking out like a sore thumb.  You may have seen other projects pick up a dependency on Hamcrest lately as well, and I bet you&#8217;re wondering what it is.  Let&#8217;s get to the bottom of it.<span id="more-220"></span></p>
<h3>Introduction</h3>
<p>The &#8220;I&#8217;m Feeling Lucky&#8221; Google search for the term <em>hamcrest</em> currently takes you to the project&#8217;s Google Code page that states Hamcrest:</p>
<blockquote><p>Provides a library of matcher objects (also known as constraints or predicates) allowing &#8216;match&#8217; rules to be defined declaratively, to be used in other frameworks.</p></blockquote>
<p>That&#8217;s a pretty good description if you already know what a Matcher is&#8230; but it doesn&#8217;t explain much for a beginner.  So what does Hamcrest really do for you?  </p>
<div id="attachment_221" class="wp-caption alignright" style="width: 210px"><a href="http://cwash.org/wp-content/uploads/2009/05/sammy.jpg"><img class="size-full wp-image-221" title="Sammy Stevens" src="http://cwash.org/wp-content/uploads/2009/05/sammy.jpg" alt="Sammy Stevens - Flea Market!" width="200" height="157" /></a><p class="wp-caption-text">It&#39;s just like... It&#39;s just like... A mini... REGEX!</p></div>
<p>A good way to think of it is that <strong>Hamcrest is to objects what regular expressions are to text</strong>.<br />
 Hamcrest provides you with a set of methods that effectively define a <strong>DSL</strong> to do <em>pattern-matching on objects</em>.</p>
<p>What can you use it for?  Well, like regular expressions, the possibilities are endless.  Some really novel uses have come about recently, but the most prominent use has been in the area of testing &#8211; hence the JUnit dependency.  </p>
<h3>A Brief History</h3>
<p>Hamcrest evolved out of the library JMock, being used to write specialized constraints on what you expected to happen to a Mock object when standing in for a real implementation.  A more fluent assertion syntax arose from these constraints which allowed you to chain together main constraint calls under a single <strong>assertThat</strong> method.  Later JMock&#8217;s author, Joe Walnes, refactored the constraint API out into its own library called Hamcrest as people became interested in using it outside of a testing framework for all kinds of different things.  He also started calling the constraints &#8220;matchers&#8221; though they can go by both names or, in some circles, &#8220;predicates.&#8221;  Other testing frameworks and even JUnit, notorious for not requiring any dependencies and staying conceptually small, have picked it up.</p>
<p>All those old friendly org.junit.Assert.* static methods are still around in JUnit 4.4+.  They&#8217;re useful for certain things, but by and large they&#8217;ve fallen out of style.  The new kid on the block, JUnit&#8217;s <strong>assertThat</strong> method allows you to pass a Matcher in to create an assertion that you would otherwise use a specialized-assert method.  Why is assertThat so useful?  A few different reasons.  Not only does it provide a more generic way to specify assertions, it also alows the framework to know specifically what you&#8217;re trying to assert.  That allows the framework to generate failure messages for you.</p>
<h3>Fail message&#8230; win?</h3>
<p>It&#8217;s always been a good idea to use the overridden version of assertX method that also takes a String for a failure message, for example:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">assertTrue<span style="color: #009900;">&#40;</span>blackbeard.<span style="color: #006633;">getOccupations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pirate&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <br />
&nbsp; &nbsp; blackbeard.<span style="color: #006633;">getOccupations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;captain&quot;</span><span style="color: #009900;">&#41;</span>, <br />
&nbsp; &nbsp; <span style="color: #0000ff;">&quot;Expected Blackbeard to be a pirate or a captain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>If we passed in an object representing a childhood version of Blackbeard, say, (before he took to the high-seas) then we&#8217;d expect to see our failure message:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">java.lang.AssertionError: Expected Blackbeard to be a pirate or a captain</div></div>
<p>It&#8217;s always been a good idea to provide these messages.  But there&#8217;s no getting around the fact that they&#8217;re repetitive, tedious, and redundant.</p>
<p>Using Hamcrest matchers alongside assertThat makes JUnit smart enough to generate meaningful failure messages for you: writing your assertion with <strong>Hamcrest matchers provides the framework with enough contextual information to generate meaningful failure messages on your behalf</strong>.  No need to maintain an extra arbitrary String to make sense of your failed assertions:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">assertThat<span style="color: #009900;">&#40;</span>blackbeard.<span style="color: #006633;">getOccupations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; anyOf<span style="color: #009900;">&#40;</span>hasItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pirate&quot;</span><span style="color: #009900;">&#41;</span>,hasItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;captain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>This may not look like much of an improvement, but it also doesn&#8217;t read any worse.    We can ditch the extra String parameter as well.  When this fails we&#8217;ll get a pretty cool failure message for free:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">java.lang.AssertionError: <br />
Expected: (a collection containing &quot;pirate&quot; or a collection containing &quot;captain&quot;)<br />
&nbsp; &nbsp; &nbsp;got: &lt; []&gt;</div></div>
<p><em>Note: I&#8217;m using JUnit 4.6 &#8211; in order to get this syntax to work you need to do some static imports:</em></p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">import static org.junit.Assert.*;<br />
import static org.hamcrest.CoreMatchers.*;<br />
import static org.junit.matchers.JUnitMatchers.*;</div></div>
<p>It looks like there&#8217;s still work being done to make this DSL read more fluently in JUnit; something like:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">assertThat<span style="color: #009900;">&#40;</span>blackbeard.<span style="color: #006633;">getOccupations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; either<span style="color: #009900;">&#40;</span>hasItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pirate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">or</span><span style="color: #009900;">&#40;</span>hasItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;captain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h3>Other Cool Stuff</h3>
<h5>Getting Reused</h5>
<p>TestNG, JMock and many other testing frameworks will certainly benefit from what Hamcrest offers, but we&#8217;re starting to see this being used in many other places as well.  I&#8217;ll be interested to see how well matchers work together or are maintained seeing that they&#8217;re relatively easy to create.  Hopefully frameworks will contribute useful matchers back to the project.</p>
<h5>Chaining</h5>
<p>Because the matchers are implemented as a DSL using the <a href="http://martinfowler.com/dslwip/MethodChaining.html">method chaining idiom</a>, they&#8217;re extremely easy to combine and extend.</p>
<h5>Matchers For Regular Expressions</h5>
<p>To bring the blog post full circle, I found this earlier tonight: the <a href="http://code.google.com/p/hamcrest-text-patterns/">Hamcrest Text Patterns project</a>.  Its goal is to have you write Hamcrest-style regular expressions using matchers which will produce much more readable regular expression code.  For example, have a look at this test that I found in the project:</p>
<div class="codecolorer-container java twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PatternMatcher emailAddressMatcher <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PatternMatcher<span style="color: #009900;">&#40;</span>sequence<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; capture<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user&quot;</span>, oneOrMore<span style="color: #009900;">&#40;</span>anyCharacter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, <br />
&nbsp; &nbsp; <span style="color: #0000ff;">&quot;@&quot;</span>, <br />
&nbsp; &nbsp; capture<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;host&quot;</span>, oneOrMore<span style="color: #009900;">&#40;</span>anyCharacter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
PatternMatcher mailToURLMatcher <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PatternMatcher<span style="color: #009900;">&#40;</span>sequence<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; capture<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;scheme&quot;</span>, text<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mailto&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, <br />
&nbsp; &nbsp; <span style="color: #0000ff;">&quot;:&quot;</span>, <br />
&nbsp; &nbsp; capture<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;email&quot;</span>, emailAddressMatcher<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
assertThat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mailto:npryce@users.sf.net&quot;</span>, matchesPattern<span style="color: #009900;">&#40;</span>mailToURLMatcher<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>By providing a friendly name for each capture (user, host, scheme, email) you can provide meaningful messages programmatically about what doesn&#8217;t match up when something doesn&#8217;t match.</p>
<h3>Conclusion</h3>
<p>I hope you choose to take a closer look at Hamcrest because it brings a big bang-for-the-buck and has the potential to guide us to more human-friendly, readable code.</p>
<h3>Further Reading</h3>
<ul>
<li>Have a look at Joe Walnes&#8217; blog for a great <a href="http://joe.truemesh.com/blog/000511.html">introduction to using assertThat</a> as well as other <a href="http://joe.truemesh.com/blog/000705.html">creative uses of Hamcrest matchers</a>.</li>
<li>Also take a look at Nat Pryce&#8217;s blog for <a href="http://www.natpryce.com/articles/000662.html">more info on Hamcrest</a> and Testing in general.</li>
</ul>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;</div></div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2009/06/03/what-is-hamcrest/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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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=What+is+Hamcrest%3F&amp;link=http://cwash.org/2009/06/03/what-is-hamcrest/&amp;notes=What%20is%20Hamcrest%3F%20%20An%20introduction%20to%20the%20constraint%2Fmatcher%2Fpredicate%20framework%20for%20Java.&amp;short_link=http://bit.ly/bJB6uY&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>June 9, 2009 -- <a href="http://cwash.org/2009/06/09/mocking-with-jmockit/" title="Mocking with JMockit">Mocking with JMockit</a> (5)</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><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>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>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>September 10, 2008 -- <a href="http://cwash.org/2008/09/10/axis2-client-using-adb-runtime-dependencies/" title="Axis2 client using ADB &#8211; runtime dependencies">Axis2 client using ADB &#8211; runtime dependencies</a> (6)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2009/06/03/what-is-hamcrest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Unit Test Anymore&#8230; No, Really!</title>
		<link>http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=dont-unit-test-anymore-no-really</link>
		<comments>http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 01:52:32 +0000</pubDate>
		<dc:creator>Chris Wash</dc:creator>
				<category><![CDATA[Developer Testing]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[Cedric Beust]]></category>
		<category><![CDATA[developer testing]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[testng]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://cwash.org/?p=133</guid>
		<description><![CDATA[Partly a response to "Your Unit Tests Lie to You," this entry explores the downstream ramifications of misuse of the term "unit testing" to refer to all automated developer testing.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>I just read <a href="http://agilesoftwaredevelopment.com/blog/janusz-gorycki/your-unit-tests-are-useless" target="_blank">Your Unit Tests Lie to You</a> by Janusz Gorycki and I was going to leave a comment there, but thought it was more appropriate to expand my comments off into their own thing.  For those that haven&#8217;t read the article, its basic premise is to grab hold of the nearest &#8220;test infected&#8221; reader and shake the warm and fuzzy out of them.  It paints the short sightedness of many recent &#8220;unit testing&#8221; converts as living in a dream world where unit tests should replace formal testing. It follows with many sentiments I&#8217;ve read (and written about here) for a while now.  It&#8217;s not that I disagree with what is being said in the article, or its tone for that matter; most of what is being said is spot on.  Unit testing is definitely not a silver bullet.  If you read my blog often, you no doubt get that. The article ends:</p>
<blockquote><p>So please, don&#8217;t fire your QA department just yet. Their job is still important, even if you unit test.</p></blockquote>
<p>So to Janusz, the fundamental problem here is a general ignorance of the purposes behind a unit test suite.  I agree 100% that&#8217;s the primary factor behind his problem.  What don&#8217;t we agree on?  Semantics.  But semantics are important!  How far do we have to go for a true zen-understanding of this issue?  Not far.  Indulge me &#8212; <span id="more-133"></span></p>
<h3>When is A Unit Test is not a Unit Test?</h3>
<p>Here&#8217;s my thesis: you may <em>use</em> a unit testing framework, but what you <em>write</em> are developer tests.  Even if they are technically unit tests, it is against everyone&#8217;s interest to call them this.  Picky, useless distinction, you say?  Hear me out.</p>
<p>There is a vast difference between the gamut of possible automated tests one could write and what is known colloquially as a unit test.  A number of different kinds of automated tests are written against frameworks that are built on top of unit testing frameworks.  That doesn&#8217;t make them unit tests.  It doesn&#8217;t make sense to <em>call</em> them unit tests.  A square is a rectangle, but does that make every rectangle a square?  An automated acceptance or integration test is subject to a completely different set of problems (in areas such as specification, maintenance, complexity) than a unit test.  In fact, about the only thing they share is their lifecycle and execution model, which many times has been <a href="http://www.youtube.com/watch?v=oGWbt3DSje0" target="_blank">retrofitted into the JUnit lifecycle and execution model</a>.</p>
<h3>&#8220;Unit Testing&#8221; and Linguistic Drift</h3>
<p>I&#8217;ve recently seen a number of different incarnations (<a href="http://www.satisfice.com/blog/archives/27" target="_blank">1</a>, <a href="http://www.computer.org/portal/cms_docs_software/software/content/best.pdf">2</a>, <a href="http://blog.exacttarget.com/blog/the-exacttarget-blog/0/0/your-opinion-is-not-best-practice-and-mine-isnt-either">3</a>) of the argument that we should eschew use of the term &#8220;best practice&#8221; because of the implications of its <a href="http://en.wikipedia.org/wiki/Linguistic_drift">linguistic drift</a> and general propensity of people to turn off their brains when just spoon-fed answers, not having to experience deriving the solution for themselves.  Similarly, the popularity of unit testing frameworks and the sheer frequency with which the term has been used have, in a sense, set the idea of developer testing back considerably.  <a href="http://www.youtube.com/watch?v=TtcEpryRy44" target="_blank">Cedric Beust makes the point</a> that in many cases we&#8217;ve confused [developer] testing terminology with JUnit terminology, and TestNG was in part a response to that.  Here we oversimplify the problems we choose to bite-off and the goals we strive toward.  They&#8217;re not realistic.  Is it no wonder so many people fall flat when trying to adopt &#8220;unit testing&#8221;?</p>
<p>While there may be some overlap with the goals of validation and verification, most in the know consider the true benefits of &#8220;unit testing&#8221; to be a totally different animal altogether.  We seem surprised to find the benefits of doing developer testing have little to do with what &#8220;testers&#8221; do.  This dischord causes a lot of confusion, and has sparked a lot of articles.  Some draw this conclusion, appropriately, that developer testing, while it fits a rigid definition of what testing is, shares little with what a typical &#8220;tester&#8221; is responsible for (true V&amp;V).  Quite often developers are the only ones doing any automation work, including this automated &#8220;developer&#8221; testing.  Developers tend to do it for all kinds of different reasons, too.  We&#8217;ll use a suite of automated tests to proceed without fear of integration errors.  That adds value <em>totally independent</em> of validation and verification practices.  If our suite catches regressions before we hand a single version off to testers, that saves both developers and testers time.  I could go on and on, but the term &#8220;unit testing&#8221; conveys very little of these kinds of benefits to the development lifecycle, and as it turns out, causes a great deal of confusion.</p>
<h3>The Right Usage</h3>
<p>When we&#8217;re talking pure <em>unit </em>tests, that is, black-box testing components in <em>pure</em> isolation (which <strong>requires isolated dependencies</strong>), not in concert, as developers we can certainly find merit in this practice.  But what we&#8217;ve learned is that this idea of unit testing, while quite beneficial as a development practice, shares very few goals with &#8220;testing&#8221; as we know it from a classical definition of the word (end-to-end V&amp;V).  Perhaps a few years ago this distinction was not that true, but we now know better.  We know the <a href="http://blog.thinkrelevance.com/2008/5/23/how-to-fail-with-100-test-coverage">danger in trying to bend unit tests into something they&#8217;re not</a>.</p>
<p>My point is essentially this: knowing how to use a unit testing framework, a very simplistic construct on the surface, to do all of these wonderful things is certainly not something that just &#8220;falls out&#8221; of developing a unit of code &#8211; it&#8217;s not something we should expect developers to just deliver each iteration as part of their deliverable like it&#8217;s &#8220;done.&#8221;  Effort invested to develop and maintain test code alongside the code under test is not free.</p>
<p>Only if you are persistent will you come to understand there sometimes is very little benefit in terms of &#8220;full blown&#8221;, classical validation and verification – the whole reason you set out on this crazy &#8220;unit testing&#8221; kick. You either stick with it, or write it off at this point.  If you stick with it,  well, I&#8217;ll save you the trouble of figuring this out for yourself; you learn it&#8217;s only another development practice, that needs to be balanced out with other sound practices. It&#8217;s hard work.   <a href="http://blog.jayfields.com/2009/02/thoughts-on-developer-testing.html">Really hard</a>.</p>
<p>Then why go to all the trouble?  It&#8217;s my contention, and I assume most will agree, that developer testing is useful.  It drives out better designs, code, and has the potential to thwart a caste of would-be regressions.  It can stamp out integration errors.  It can help you learn about a new framework or prototype a new feature.  But that usefulness comes with a very real cost.</p>
<p>If we truly acknowledge that the type of practice we&#8217;ve talked about has merit as a development process, it makes very little sense to continue referring to the practice as &#8220;unit testing.&#8221;  If testing modules of code, driving development with these tests, maintaining them alongside the code, and other automation activities are worthy practice <em>for developers</em>, we should consider the semantics of our vocational vocabulary.  Let&#8217;s stop confusing each other.  We&#8217;re not talking about delivering 100% code coverage, we&#8217;re not talking about replacing testers (unless you&#8217;ve got the huevos to go without them, anyway), and we&#8217;re surely not talking about writing <em>or releasing</em> bug-free software.  It&#8217;s a lot easier to cut through a lot of the hype and confusion if we can all learn to say developer testing and understand it as such.  Let&#8217;s put the term &#8220;unit test&#8221; to rest.</p>
<p>R.I.P. unit tests.  Long live &#8220;developer tests!&#8221;</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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=Don%27t+Unit+Test+Anymore...+No%2C+Really%21&amp;link=http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/&amp;notes=Partly%20a%20response%20to%20%22Your%20Unit%20Tests%20Lie%20to%20You%2C%22%20this%20entry%20explores%20the%20downstream%20ramifications%20of%20misuse%20of%20the%20term%20%22unit%20testing%22%20to%20refer%20to%20all%20automated%20developer%20testing.&amp;short_link=http://bit.ly/bxvaIa&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>June 9, 2009 -- <a href="http://cwash.org/2009/06/09/mocking-with-jmockit/" title="Mocking with JMockit">Mocking with JMockit</a> (5)</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>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>March 13, 2008 -- <a href="http://cwash.org/2008/03/13/continuous-integration-dissected/" title="Continuous Integration Dissected">Continuous Integration Dissected</a> (0)</li><li>March 11, 2008 -- <a href="http://cwash.org/2008/03/11/bugs-detectives-and-test-automation/" title="Bugs, Detectives, and Test Automation">Bugs, Detectives, and Test Automation</a> (2)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://cwash.org/2009/02/17/dont-unit-test-anymore-no-really/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

