<?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>The Shyam &#187; mock</title>
	<atom:link href="http://theshyam.com/tag/mock/feed/" rel="self" type="application/rss+xml" />
	<link>http://theshyam.com</link>
	<description>Ramblings from the Real Shyam; You know, unlike those other fake Shyams!</description>
	<lastBuildDate>Sun, 08 Jan 2012 13:07:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Fakes and Mocks and Stubs, Oh My!!!</title>
		<link>http://theshyam.com/2008/08/fakes-and-mocks-and-stubs-oh-my/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fakes-and-mocks-and-stubs-oh-my</link>
		<comments>http://theshyam.com/2008/08/fakes-and-mocks-and-stubs-oh-my/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 03:50:10 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[Technique]]></category>
		<category><![CDATA[fake]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[stub]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://assertionfailed.wordpress.com/?p=42</guid>
		<description><![CDATA[So we covered how to use EasyMock to write tests in one of the previous posts, but Mocking is not the only option when you want to test something that depends on a slow and expensive service. Mocking allows you to expect calls and return values when a call is made, and make sure the [...]]]></description>
			<content:encoded><![CDATA[<p>So we covered how to use EasyMock to write tests in one of the previous posts, but Mocking is not the only option when you want to test something that depends on a slow and expensive service. Mocking allows you to expect calls and return values when a call is made, and make sure the number of calls were correct and the order, but sometimes, you want a bit more, or not even that much. So in those cases, you have the options of using Stubs or Fakes. So without further delay, your options are :</p>
<ol>
<li><strong>Mocks </strong>:<br />
Well, Mocks&#8230; What can I say. I have already ranted and raved about the awesomeness that is Mocks. Mocks and Mocking frameworks allow you to replace heavy and expensive services with Mocks, with which you can <strong>set expectations </strong>on what calls are made to the service, and return values. This gives you the advantage of knowing exactly what is called and returned, and makes your test deterministic and super fast. You can even set expectations on the number of calls and the order, though this is something that is not extremely recommended, since that makes the test dependent on the implementation, which is never a good thing.<em><strong> </strong></em></p>
<p><em><strong>Advantages</strong></em> :</p>
<ul>
<li>Fast, Reliable</li>
<li>Deterministic</li>
<li>Lightweight</li>
<li>Control over expectations and return values.</li>
</ul>
<p><em><strong> Disadavantages</strong></em> :</p>
<ul>
<li>Can become dependent on implementation of method being tested.</li>
<li>Can become a mockery if not careful, that is, can be testing interaction between mocks and nothing of the actual code. Especially when all that the method under test is doing is delegating to a bunch of service calls.</li>
<li>Can involve complicated setup for expectations, especially with unwieldy objects</li>
</ul>
</li>
<li><strong>Stubs</strong> :<br />
Now, whereas with Mocks, you can specify what calls will be received and what will be returned making them somewhat intelligent, Stubs are at the other end of the spectrum. The dumbest of the dumb, the easiest of the easiest, these just stub out the methods of your expensive and heavy class. So a <strong>null Stub </strong>could return null for each method, or just return some constant value each time it is called. That means that regardless of what the method is called with, it <strong>returns the exact same thing, </strong>day in and day out. It doesn&#8217;t adjust, it doesn&#8217;t react, just returns. Of course, this means that you might not be able to test all the cases you want to inspect / test. But its simple, its easy, and of course, its fast.<em><strong> </strong></em></p>
<p><em><strong>Advantages</strong></em> :</p>
<ul>
<li>Fast, reliable</li>
<li>Simple</li>
<li>Consistent</li>
</ul>
<p><em><strong> Disadvantages </strong></em>:</p>
<ul>
<li>Dumb</li>
<li>Can&#8217;t exercise different cases without different stubs</li>
<li>Returns only one value consistently</li>
</ul>
</li>
<li><strong>Fakes</strong> :<br />
And finally, we come to the smartest (figuratively) of the lot. The Fakes. For once, being a fake can be good. While Stubs return the same thing again and again, and mocks return what you tell it to, Fakes are smarter. The easiest way to explain a Fake is with an example. Say your code depends on a heavy service like a Database. A <strong>FakeDatabase </strong>would be an <strong>in-memory implementation </strong>of the DB, thus making it faster while at the same time, providing the same logic as the normal DB would. There are different types of Fakes, like a <strong>listening / recording Fake </strong>which records all the values passed to it.<em><strong> </strong></em></p>
<p><em><strong>Advantages</strong></em> :</p>
<ul>
<li>Can test while preserving behavior of dependencies</li>
<li>Faster than using actual services</li>
<li>Can test complicated logic</li>
<li>Most comprehensive testing approach</li>
</ul>
<p><em><strong> Disadvantages</strong></em> :</p>
<ul>
<li>Can be complicated to set up</li>
<li>Not as fast as mocks / stubs</li>
<li>Not as easy to define expectations</li>
<li>When / Where do you test the fake (especially the complicated ones) ?</li>
</ul>
</li>
</ol>
<p>Thats my 2 cents on the different approaches to testing with dependencies. Now that we know what we can do with those darn heavy dependencies, I&#8217;ll talk more on how to make sure you can use these different approaches to test your code soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://theshyam.com/2008/08/fakes-and-mocks-and-stubs-oh-my/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Mock, Mock! Who&#8217;s there ?</title>
		<link>http://theshyam.com/2008/08/mock-mock-whos-there/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mock-mock-whos-there</link>
		<comments>http://theshyam.com/2008/08/mock-mock-whos-there/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 04:59:27 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[Tool]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[easymock]]></category>
		<category><![CDATA[fake]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[stub]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://assertionfailed.wordpress.com/?p=31</guid>
		<description><![CDATA[The concept of mocking is not a new idea, but its one that has been gaining traction recently. But still, whenever I tell people to mock out their dependencies when they are trying to test, they look at me as if wondering what the heck I&#8217;m smoking. Well, if I was smoking something, I would [...]]]></description>
			<content:encoded><![CDATA[<p>The concept of mocking is not a new idea, but its one that has been gaining traction recently. But still, whenever I tell people to mock out their dependencies when they are trying to test, they look at me as if wondering what the heck I&#8217;m smoking. Well, if I was smoking something, I would share it. But mocking is a great thing for writing small unit tests.</p>
<p>I get usually one of the two statements / questions when I tell someone to just mock something out.</p>
<ol>
<li>Well, if I am mocking things out, then I am not really testing it, am I ? OR I don&#8217;t want to mock things out. I need to test the method&#8217;s interaction with other classes.</li>
<li>What about the mocked class ? We have to make sure it works too.</li>
</ol>
<p>Well, to number 1, I say, if by mocking things out, you have nothing but a series of expected calls and returns, and there&#8217;s no actual class specific behavior there, then does that Class really deserve to be a class ?</p>
<p>And the second part of number 1, well&#8230;. Is that really a unit test then ? Ideally, you should have a lot of unittests to make sure the class specific logic is sound, and then a few integration tests to make sure that everything is hooked up correctly.</p>
<p>And number 2&#8230; This is where you go and write unit tests for the class you have just mocked. Don&#8217;t depend on large scale integration tests to test all your classes. Write nice small unit tests which are fast and precise. The larger a test gets, the harder it is to find why a particular test broke and how.</p>
<p>So hopefully by now, I have you convinced that mocking may not be all that bad. Fine, you say, so how do I go about this mocking thing ? Glad you asked. While there are many mocking frameworks out there, I am going to just talk about EasyMock for Java. JMock is very similar and could be pretty interchangable.</p>
<p>The first requirement before you can use any mocking framework is the ability to inject mocks into your class. This is <strong>Dependency Injection</strong> at its core, without which you will have to find workarounds like protected setter methods and the like. But basically, if a class uses some Service or Database, make sure that you can override it with a Mock Database by passing it into a constructor or setting it via a method.</p>
<p>Once thats done, the first step in using a mock is <strong>creating the mock object.</strong> In EasyMock, its as simple as :</p>
<blockquote><p><em>MyClass myObject = EasyMock.createMock(MyClass.class);</em></p></blockquote>
<p>Thats it, nothing fancier than that. It helps if MyClass is an interface, but I believe EasyMock supports mocking non interface classes as well. Once you have done that, you can inject this mock object into the classes which you are testing.</p>
<p>The next step is <strong>setting expectations. </strong>For void methods, it as simple as just calling the method with the expected parameter. For example :</p>
<blockquote><p><em>myObject.myVoidMethod(&#8220;ThisStringWillBePassed&#8221;);<br />
EasyMock.expect(myObject.methodWithReturnValue(&#8220;ExpectedArg&#8221;)).andReturn(&#8220;ReturnValue&#8221;);<br />
EasyMock.replay(myObject);</em></p></blockquote>
<p>The EasyMock.replay(myObject) tells EasyMock that you are done setting expectations and that the next time a method on the object is called, treat it as an actual call. So then in your test, you proceed as normal invoking the methods you care about, and then finally, you call :</p>
<blockquote><p><em>EasyMock.verify(myObject);</em></p></blockquote>
<p>This ensures that all the <strong>expectations </strong>set on myObject were <strong>met</strong>. Now EasyMock also supports additional features like setting expectations on number of method calls, throwing exceptions, flexible argument matchers and so much more. For more information, check out the <a title="EasyMock" href="http://www.easymock.org" target="_blank">EasyMock home page</a>.</p>
<p>Now a few caveats with regards to mocking. It is very easy to degenerate some tests into what we call a <strong>mockery,</strong> where we end up testing mocks and their interaction instead of the actual class we want to test. So Don&#8217;t overuse mocks. Use them when you have to test something which depends on Database or expensive service calls. Also if your test ends up exercising a bunch of mock calls and nothing else, that might be a hint that your class really does not belong. And of course, it goes without saying that don&#8217;t mock the class you are testing.</p>
<p>Also, don&#8217;t set up Mock layers where a class which indirectly depends on some service object uses the mock layer. You should always mock the classes which your Class Under Test directly depends on, and not classes which it indirectly depends on. And sometimes, a mock might not be what you are looking for. Instead, a simple Stub or a Fake might be more useful. I might talk more on this or Dependency Injection in my next post.</p>
]]></content:encoded>
			<wfw:commentRss>http://theshyam.com/2008/08/mock-mock-whos-there/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

