<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David Rey on Medium]]></title>
        <description><![CDATA[Stories by David Rey on Medium]]></description>
        <link>https://medium.com/@dreyacosta?source=rss-763afc4bf3e1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*3emSz12LO9NHCxbzTOEmMA.png</url>
            <title>Stories by David Rey on Medium</title>
            <link>https://medium.com/@dreyacosta?source=rss-763afc4bf3e1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 20:23:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dreyacosta/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Not all doubles are Mocks]]></title>
            <link>https://engineering.audiense.com/not-all-doubles-are-mocks-d522d694fa8f?source=rss-763afc4bf3e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/d522d694fa8f</guid>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[David Rey]]></dc:creator>
            <pubDate>Fri, 13 Jan 2017 10:17:09 GMT</pubDate>
            <atom:updated>2017-01-13T10:29:32.691Z</atom:updated>
            <content:encoded><![CDATA[<p>When we carry out a unit test for a class, object or module, i.e., our Subject<br>Under Test (SUT), we want to simulate its collaborators/dependencies in order to avoid slow tests or infrastructure costs that we can’t afford.</p><p>However, there is a lot of confusion in this area and it’s very common to call<br>these pieces of software Mocks. But, as Martin Fowler said, Mocks aren’t<br>doubles.</p><blockquote>The vocabulary for talking about this soon gets messy — all sorts of words are used: stub, mock, fake, dummy. For this article I’m going to follow the vocabulary of Gerard Meszaros’s book. It’s not what everyone uses, but I think it’s a good vocabulary and since it’s my essay I get to pick which words to use.</blockquote><blockquote>Meszaros uses the term Test Double as the generic term for any kind of pretend object used in place of a real object for testing purposes.</blockquote><p>Test double is the generic term and then we have specific terms such as dummy, stub, spy, mock, etc., which are very similar but which have different purposes.</p><p>Some tools and frameworks such as mockito don’t help to clarify this confusion when everything you do is through Mocks because they make no distinction between Stubs and Mocks.</p><p>We are going to try the see the differences between test doubles like dummy, stub and mock through an example.</p><p>Consider the following Subject Under Test (SUT):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d25aa589fb5530872d826f1320921a2/href">https://medium.com/media/6d25aa589fb5530872d826f1320921a2/href</a></iframe><p>This SUT needs a collaborator to be created. Then it has a<br><strong>collaboratorNotNeeded</strong> method where a collaborator is not needed and a<br><strong>collaboratorNeeded</strong> method where one is needed.</p><p>The collaborator looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e1bd186d0179c246f2032912979a3e6/href">https://medium.com/media/7e1bd186d0179c246f2032912979a3e6/href</a></iframe><p>The collaborator has a `setup` method for some initialisation which is the first thing that the SUT does.</p><p>Then we have the `doSomethingImportant` method which calls a private function that carries out very heavy workload operations that take several minutes to complete.</p><p>It is very important to respect the interface of the collaborator for unit<br>tests. So, let’s take a look at these tests:</p><h3>Dummy</h3><p>This is the simplest test of all. It has to implement the required interface so<br>that we can create our SUT. It’s a placeholder but, in the context of SUT, it<br>doesn’t need to be used.</p><p>Let’s have a look at our test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fac736cc06782e747cb7bc51f8245fb7/href">https://medium.com/media/fac736cc06782e747cb7bc51f8245fb7/href</a></iframe><p>Basically just enough code so we can create the SUT in order to test the method where a collaborator is not needed.</p><h3>Stub</h3><p>This provides canned answers to calls made during the test. It has a simplified implementation of the behaviour only for the purposes of testing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66b34a47c38e83c7dd309025172dc40f/href">https://medium.com/media/66b34a47c38e83c7dd309025172dc40f/href</a></iframe><p>The Stub has a much lighter implementation of the real <strong>doSomethingImportant</strong> method so that we can test the <strong>collaboratorNeeded</strong> method of the SUT at a lower cost.</p><h3>Mock</h3><p>As Martin Fowler said:</p><blockquote>“Mocks are […] objects pre-programmed with expectations which form a specification of the calls they are expected to receive.”</blockquote><p>A Mock is very complex. It allows us to check which methods are called, the params of the methods, the order of the params, etc… It adds a lot of complexity to our tests and makes it difficult to follow.</p><p>This is our small implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/256971ee9238c725fff908f99937ee81/href">https://medium.com/media/256971ee9238c725fff908f99937ee81/href</a></iframe><h3>Conclusion</h3><p>Test doubles are an essential part of unit testing. Having a good understanding of dummies, stubs, mocks and all the kinds of Test doubles out there is fundamental in order to build effective tests that correctly check system behaviour.</p><p>Which type of double should you use? It depends on the code and what you expect from the test. In my opinion it’s best to avoid the use of Mocks in favour of Dummies and Stubs. You will almost never need to make a Mock if you have a SOLID design. Mocks are ‘smells’. If you need them you probably have a problem with your design or architecture that shouldn’t be resolved with tools.</p><h4>Bibliography</h4><p>- <a href="http://martinfowler.com/articles/mocksArentStubs.html">Mocks Aren’t Stubs</a> by Martin Fowler<br>- <a href="https://blog.8thlight.com/uncle-bob/2014/05/14/TheLittleMocker.html">The Little Mocker</a> by Uncle Bob</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d522d694fa8f" width="1" height="1" alt=""><hr><p><a href="https://engineering.audiense.com/not-all-doubles-are-mocks-d522d694fa8f">Not all doubles are Mocks</a> was originally published in <a href="https://engineering.audiense.com/">Audiense Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>