<p>When I originally was introduced to Mocks I felt the primary purpose was to mock up objects that come from external sources of data.  This way I did not have to maintain an automated unit testing test database, I could just fake it.</p>

<p>But now I am starting to think of it differently.  I am wondering if Mocks are more effective used to completely isolate the tested method from anything outside of itself.  The image that keeps coming to mind is the backdrop you use when painting.  You want to keep the paint from getting all over everything.  I am only testing that method, and I only want to know how it reacts to these faked up external factors?</p>

<p>It seems incredibly tedious to do it this way but the advantage I am seeing is when the test fails it is because it is screwed up and not 16 layers down.  But now I have to have 16 tests to get the same testing coverage because each piece would be tested in isolation.  Plus each test becomes more complicated and more deeply tied to the method it is testing.</p>

<p>It feels right to me but it also seems brutal so I kind of want to know what others think.</p>

[{'answer_id': 59212, 'author': 'Sam Wessel', 'author_id': 4734, 'author_profile': 'https://Stackoverflow.com/users/4734', 'pm_score': 2, 'selected': False, 'text': '<p>Yes, I agree. I see mocking as sometimes painful, but often necessary, for your tests to truly become <strong>unit</strong> tests, i.e. only the smallest unit that you can make your test concerned with is under test. This allows you to eliminate any other factors that could potentially affect the outcome of the test. You do end up with a lot more small tests, but it becomes so much easier to work out where a problem is with your code.</p>\n'}, {'answer_id': 59244, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 4, 'selected': False, 'text': '<p>Don\'t go down the dark path Master Luke. :) Don\'t mock everything. You could but you shouldn\'t... here\'s why.</p>\n\n<ul>\n<li>If you continue to test each method in isolation, you have surprises and work cut out for you when you bring them all together ala the <strong>BIG BANG</strong>. We build objects so that they can work together to solve a bigger problem.. By themselves they are insignificant. You <strong>need to know if all the collaborators are working as expected</strong>.</li>\n<li>Mocks <strong>make tests brittle</strong> by introducing duplication - Yes I know that sounds alarming. For every mock expect you setup, there are n places where your method signature exists. The actual code and your mock expectations (in multiple tests). Changing actual code is easier... updating all the mock expectations is tedious. </li>\n<li>Your <strong>test is now privy to insider implementation information</strong>. So your test depends on how you chose to implement the solution... bad. Tests should be a independent spec that can be met by multiple solutions. I should have the freedom to just press delete on a block of code and reimplement <strong>without</strong> having to rewrite the test suite.. coz the requirements still stay the same.</li>\n</ul>\n\n<p>To close, I\'ll say "If it quacks like a duck, walks like a duck, then it probably is a duck" - If it feels wrong.. it probably is. *Use mocks to abstract out problem children like IO operations, databases, third party components and the like.. Like salt, some of it is necessary.. too much and :x *<br>\nThis is the holy war of State based vs Iteraction based testing.. Googling will give you deeper insight.</p>\n\n<p>Clarification: I\'m hitting some resistance w.r.t. integration tests here :) So to clarify my stand.. </p>\n\n<ul>\n<li>Mocks do not figure in the \'Acceptance tests\'/Integration realm. You\'ll only find them in the Unit Testing world.. and that is my focus here.</li>\n<li>Acceptance tests are different and are <strong>very much</strong> needed - not belittling them. But Unit tests and Acceptance tests are different and should be kept different.</li>\n<li>All collaborators within a component or package do not need to be isolated from each other.. Like micro-optimization that is Overkill. They exist to solve a problem <strong>together</strong>.. cohesion. </li>\n</ul>\n'}, {'answer_id': 59255, 'author': 'asksol', 'author_id': 5577, 'author_profile': 'https://Stackoverflow.com/users/5577', 'pm_score': 1, 'selected': False, 'text': "<p>My philosophy is that you should write testable code to fit the tests,<br>\nnot write tests to fit the code. </p>\n\n<p>As for complexity, my opinion is that tests should be simple to write, simply because you write more tests if they are.</p>\n\n<p>I might agree that could be a good idea if the classes you're mocking doesn't have a test suite, because if they did have a proper test suite, you would know where the problem is without isolation.</p>\n\n<p>Most of them time I've had use for mock objects is when the code I'm writing tests for is so tightly coupled (read: bad design), that I have to write mock objects when classes they depend on is not available. Sure there are valid uses for mock objects, but if your code <strong>requires</strong> their usage, I would take another look at the design.</p>\n"}, {'answer_id': 59266, 'author': 'Justin Standard', 'author_id': 92, 'author_profile': 'https://Stackoverflow.com/users/92', 'pm_score': 5, 'selected': True, 'text': '<p>I recommend you take a look at Martin Fowler\'s article <a href="http://www.martinfowler.com/articles/mocksArentStubs.html" rel="noreferrer"><em>Mocks Aren\'t Stubs</em></a> for a more authoritative treatment of Mocks than I can give you.</p>\n\n<p>The purpose of mocks is to unit test your code in isolation of dependencies so you can truly test a piece of code at the "unit" level.  The code under test is the real deal, and every other piece of code it relies on (via parameters or dependency injection, etc) is a "Mock" (an empty implementation that always returns expected values when one of its methods is called.)</p>\n\n<p>Mocks may seem tedious at first, but they make Unit Testing far easier and more robust once you get the hang of using them.  Most languages have Mock libraries which make mocking relatively trivial.  If you are using Java, I\'ll recommend my personal favorite: <a href="http://www.easymock.org/" rel="noreferrer">EasyMock.</a></p>\n\n<p>Let me finish with this thought: you need integration tests too, but having a good volume of unit tests helps you find out which component contains a bug, when one exists.</p>\n'}, {'answer_id': 59293, 'author': 'Spoike', 'author_id': 3713, 'author_profile': 'https://Stackoverflow.com/users/3713', 'pm_score': 1, 'selected': False, 'text': "<p>Yes, that is the downside of testing with mocks. There is a lot of work that you need to put in that it feels brutal. But that is the essence of unit testing. How can you test something in isolation if you don't mock external resources?</p>\n\n<p>On the other hand, you're mocking away slow functionality (such as databases and i/o operations). If the tests run faster then that will keep programmers happy. There is nothing much more painful than waiting for really slow tests, that take more than 10 seconds to finish running, while you're trying to implement one feature.</p>\n\n<p>If every developer in your project spent time writing unit tests, then those 16 layers (of indirection) wouldn't be that much of a problem. Hopefully you should have that test coverage from the beginning, right? :)</p>\n\n<p>Also, don't forget to write a function/integration test between objects in collaboration. Or else you might miss something out. These tests won't need to be run often, but are still important.</p>\n"}, {'answer_id': 59360, 'author': 'George Mauer', 'author_id': 5056, 'author_profile': 'https://Stackoverflow.com/users/5056', 'pm_score': 1, 'selected': False, 'text': "<p>On one scale, yes, mocks are meant to be used to simulate external data sources such as a database or a web service.  On a more finely grained scale however if you're designing loosely coupled code then you can draw lines throughout your code almost arbitrarily as to what might at any point be an 'outside system'.  Take a project I'm working on currently:</p>\n\n<p>When someone attempts to check in, the <strong>CheckInUi</strong> sends a <strong>CheckInInfo</strong> object to a <strong>CheckInMediator</strong> object which validates it using a <strong>CheckInValidator</strong>, then if it is ok, it fills a domain object named <strong>Transaction</strong> with <strong>CheckInInfo</strong> using <strong>CheckInInfoAdapter</strong> then passes the <strong>Transaction</strong> to an instance of <strong>ITransactionDao.SaveTransaction()</strong> for persistence. </p>\n\n<p>I am right now writing some automated <em>integration tests</em> and obviously the <strong>CheckInUi</strong> and <strong>ITransactionDao</strong> are windows unto external systems and they're the ones which should be mocked.  However, whose to say that at some point <strong>CheckInValidator</strong> won't be making a call to a web service?   That is why when you write <em>unit tests</em> you assume that everything other than the specific functionality of your class is an external system.  Therefore in my unit test of <strong>CheckInMediator</strong> I mock out all the objects that it talks to.</p>\n\n<p><strong>EDIT:</strong> Gishu is technically correct, not everything needs to be mocked, I don't for example mock <strong>CheckInInfo</strong> since it is simply a container for data.  However anything that you could ever see as an external service (and it is almost anything that transforms data or has side-effects) should be mocked.</p>\n\n<p>An analogy that I like is to think of a properly loosely coupled design as a field with people standing around it playing a game of catch.  When someone is passed the ball he might throw a completely different ball to the next person, he might even throw a multiple balls in succession to different people or throw a ball and wait to receive it back before throwing it to yet another person.   It is a strange game.  </p>\n\n<p>Now as their coach and manager, you of course want to check how your team works as a whole so you have team practice (integration tests) but you also have each player practice on his own against backstops and ball-pitching machines (unit tests with mocks).  The only piece that this picture is missing is mock expectations and so we have our balls smeared with black tar so they stain the backstop when they hit it.  Each backstop has a 'target area' that the person is aiming for and if at the end of a practice run there is no black mark within the target area you know that something is wrong and the person needs his technique tuned.</p>\n\n<p>Really take the time to learn it properly, the day I understood Mocks was a huge a-ha moment.  Combine it with an inversion of control container and I'm never going back.</p>\n\n<p>On a side note, one of our IT people just came in and gave me a free laptop!</p>\n"}, {'answer_id': 121814, 'author': 'Korbin', 'author_id': 17902, 'author_profile': 'https://Stackoverflow.com/users/17902', 'pm_score': 1, 'selected': False, 'text': "<p>As someone said before, if you mock everything to isolate more granular than the class you  are testing, you give up enforcing cohesion in you code that is under test. </p>\n\n<p>Keep in mind that mocking has a fundamental advantage, behavior verification. This is something that stubs don't provide and is the other reason that makes the test more brittle (but can improve code coverage).</p>\n"}, {'answer_id': 215113, 'author': 'keithb', 'author_id': 29229, 'author_profile': 'https://Stackoverflow.com/users/29229', 'pm_score': 1, 'selected': False, 'text': '<p>Mocks were invented in part to <a href="http://peripateticaxiom.blogspot.com/2008/06/tdd-mocks-and-design.html" rel="nofollow noreferrer">answer the question</a>: How would you unit test objects if they had no getters or setters?</p>\n\n<p>These days, <a href="http://www.infoq.com/presentations/Mock-Objects-Nat-Pryce-Steve-Freeman" rel="nofollow noreferrer">recommended</a> practice is to mock roles not objects. Use Mocks as a design tool to talk about collaboration and separation of responsibilities, and not as "smart stubs". </p>\n'}, {'answer_id': 3276296, 'author': 'koen', 'author_id': 141346, 'author_profile': 'https://Stackoverflow.com/users/141346', 'pm_score': 0, 'selected': False, 'text': '<p>Mock objects are 1) often used as a means to isolate the code under test, BUT 2) as keithb already pointed out, are important to "<a href="http://blog.dannorth.net/2008/09/14/the-end-of-endotesting/#comment-7954" rel="nofollow noreferrer">focus on the relationships between collaborating objects</a>". This article gives some insights and history related to the subject: <a href="http://www.methodsandtools.com/archive/archive.php?id=90" rel="nofollow noreferrer">Responsibility Driven Design with Mock Objects</a>.</p>\n'}]
['https://Stackoverflow.com/questions/59195', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5551/']