<?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>Masterzen's Blog &#187; Testing</title>
	<atom:link href="http://www.masterzen.fr/category/programming/testing-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.masterzen.fr</link>
	<description>Journey in a software world...</description>
	<lastBuildDate>Sat, 31 Jul 2010 15:48:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How do you like your Mocks served?</title>
		<link>http://www.masterzen.fr/2009/01/16/how-do-you-like-your-mocks-served/</link>
		<comments>http://www.masterzen.fr/2009/01/16/how-do-you-like-your-mocks-served/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 21:28:14 +0000</pubDate>
		<dc:creator>masterzen</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.masterzen.fr/?p=9</guid>
		<description><![CDATA[I like them refreshing, of course:

Mockito is the new Java mock library on the block, with lots of interesting features. It replaced JMock in almost all my Java projects, mainly because:

the syntax produces clear and readable test code (see below for an example), because it doesn&#8217;t abuse of anonymous class and methods are really methods.
stub [...]]]></description>
			<content:encoded><![CDATA[<p>I like them refreshing, of course:</p>
<p style="text-align: center;"><img class="aligncenter" src="http://mockito.googlecode.com/svn/wiki/images/logo.jpg" alt="" width="347" height="161" /></p>
<p><a href="http://code.google.com/p/mockito/" onclick="javascript:pageTracker._trackPageview('/outbound/article/code.google.com');">Mockito </a>is the new Java <a href="http://en.wikipedia.org/wiki/Mock_Object" onclick="javascript:pageTracker._trackPageview('/outbound/article/en.wikipedia.org');">mock library</a> on the block, with lots of interesting features. It replaced JMock in almost all my Java projects, mainly because:</p>
<ul>
<li>the syntax produces<em> clear and readable test code</em> (see below for an example), because it doesn&#8217;t abuse of anonymous class and methods are really methods.</li>
<li>stub and verification happens <em>logically</em>, and at different place</li>
<li><em>no replay</em> or framework control methods ala EasyMock</li>
<li>fully integrated to Junit (using @RunWith for instance)</li>
<li>helpful <em>annotations to create mock</em> automagically</li>
<li>it promotes <em>simple tests by nature</em> (and that&#8217;s essential to my eyes)</li>
</ul>
<p>Basically, you can only do two things with <em>Mockito</em>:</p>
<ul>
<li>stub, or</li>
<li>verify <img src='http://www.masterzen.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
</ul>
<p>Enough discussion, let&#8217;s focus on an example:</p>
<pre class="syntax-highlight:java">
@Test
public void itShouldComputeAndSetThePlayerRank()
{
  // creating a mock from an interface
  // is as easy as that:
  Player p = mock(Player.class)

  // stub a method
  when(p.getScore()).thenReturn(5);

  // our imaginary SUT
  ELOCalculator.computeRank(p);

  // let&#039;s verify our rank has been computed
  verify(p).setRank(12);
}
</pre>
<p>Due to its use of Generics and Java 5 autoboxing, the syntax is very clean, clear and readable.<br />
But that&#8217;s not all, <em>Mockito</em> provide a <em>Junit 4</em> runner that simplifies mock creation with the help of annotations:</p>
<pre class="syntax-highlight:java">
@RunWith(MockitoJUnit44Runner.class)
public class OurImaginaryTestCase
{
  @Mock
  private Player player;

  @Test
  public void playerShouldBeRanked()
  {
     // we can use player directly here,
     // it is mocked to the Player Interface
  }
}
</pre>
<p>Of course during the verification phase of the test you can check for</p>
<ul>
<li> the number of calls (or check for no calls at all)</li>
<li>the arguments (Mockito defines lots of useful arguments matcher, and you can plug any Hamcrest matchers),</li>
<li>the call order,</li>
<li>and for stubbing, you can also throw exception, return values, or define callbacks that will be called when a return value is needed.</li>
</ul>
<p>In a word it&#8217;s really powerful.</p>
<p>It is also possible to <a href="http://xunitpatterns.com/Test%20Spy.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/xunitpatterns.com');">spy on concrete objects</a> however as the manual says <a href="http://groups.google.com/group/mockito/browse_thread/thread/3945fe1eca2954e7/007b58d8c2a42cb8?lnk=gst&amp;q=partial+mocking#007b58d8c2a42cb8" onclick="javascript:pageTracker._trackPageview('/outbound/article/groups.google.com');">this is not partial mocking</a>: so you can&#8217;t use this method to check that the method under test calls other methods of the same object.</p>
<p>Here&#8217;s an example of what I mean (the following test passes):</p>
<pre class="syntax-highlight:java">
public class RealObject {
  public int a() {
    return 10;
  }
  public int b() {
    return 20 + a();
  }
}

@Test
public final void test1()
{
  RealObject real = new RealObject();
  RealObject spy = spy(real);

  when(spy.a()).thenReturn(12);

  // notice the 30 here
  assertThat(spy.b(), equalTo(30));
}
</pre>
<p>See <a href="http://monkeyisland.pl/2009/01/13/subclass-and-override-vs-partial-mocking-vs-refactoring/" onclick="javascript:pageTracker._trackPageview('/outbound/article/monkeyisland.pl');">Mockito author&#8217;s last blog post</a> about the subject or this <a href="http://groups.google.com/group/mockito/browse_thread/thread/d856d10824027f58#" onclick="javascript:pageTracker._trackPageview('/outbound/article/groups.google.com');">mockito mailing list post</a>. Basically the code should be refactored or we could use a subclass to overcome this.</p>
<p>There is also a debate about stubbing and verifying (the same call). Usually you don&#8217;t want to do that. Stubbing should be enough, if you&#8217;re code succeed then the call was implicitly verified. So usually if you stub there is no need to verify, and if you verify you don&#8217;t need to stub (except if you need to return something critical to the rest of the code, in which case you don&#8217;t need verification).</p>
<p>Once again, <a href="http://monkeyisland.pl/2008/04/26/asking-and-telling/" onclick="javascript:pageTracker._trackPageview('/outbound/article/monkeyisland.pl');">Mockito&#8217;s author has a great post on the <em>stubbing or verifying debate</em></a>.</p>
<p>Of course if you are an <em>Eclipse</em> user, do not forget to add to the list of Favorites all Mockito static import, so that Content Assist knows all the matchers.</p>
<p>Happy unit testing with Mockito <img src='http://www.masterzen.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.masterzen.fr/2009/01/16/how-do-you-like-your-mocks-served/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.114 seconds -->
