<p>Which scenarios, areas of an application/system, etc. are best suited for 'classic' state based testing versus using mock objects?</p>

[{'answer_id': 54947, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': True, 'text': "<p>You should be using mocks for dependencies.  I don't think that its an either-or; Usually you will create mocks for dependencies, set expectations (whether it is calls or state) on them, then run the unit under test.  Then you would check its state, and verify the expectations on the mocks, afterwards.</p>\n"}, {'answer_id': 54976, 'author': 'Kilhoffer', 'author_id': 5469, 'author_profile': 'https://Stackoverflow.com/users/5469', 'pm_score': 2, 'selected': False, 'text': "<p>Using mock objects doesnt mean you're not doing state based testing.</p>\n"}, {'answer_id': 55091, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 0, 'selected': False, 'text': "<p>Its a matter of style.. Mockist vs Classic TDDers.<br>\nPersonally.. I'd go testing real classes as far as possible. Tone down on Mocks as much as possible; only to decouple things like IO (filesystems, DB Connections, network), Third party components, etc. things that are slow/difficult to get under test. </p>\n"}, {'answer_id': 57823, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>As an experienced TDD\'er, this is a question that I\'m frequently asked by other developers. For me, it\'s a mistake to get sucked into a Mockist vs. Classicist debate, as such a discussion is misleading. State-based and behaviuour-based unit testing are two different tools in your toolbox, and there\'s no reason why they should be mutually exclusive.</p>\n\n<p>State-based unit testing is suitable when you want to query the internal properties of an object after talking to its external interface. If one or more of its collaborators involve potentially expensive calls to other objects, then by all means stub those calls and simply disregard collaborators. </p>\n\n<p>Behaviour-based unit testing is a good idea when you want to consider the "how" of a unit test and focus upon discoving relationships between objects, as opposed to the traditional "what" questions of a state-based unit test. If you wish to assert that collaborators are used in a certain way and/or sequential order, use mocks - stubs that provide assertions.</p>\n\n<p>I suggest you focus upon standard unit testing practices - exercise as little production code as possible, and have <em>one</em> assertion per test. This will force you to think "what exactly is it that I want to exercise and assert in this test?", and the answer to that question will help you choose the correct unit testing tools.</p>\n'}, {'answer_id': 58485, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m going to tackle it from a TDD/BDD perspective, where the tests are driving the designs.</p>\n\n<p>First off it depends what style of design you buy into, because remember this is about design first.  As Martin Fowler discusses in this excellent article <a href="http://www.martinfowler.com/articles/mocksArentStubs.html" rel="noreferrer">Mocks Aren\'t Stubs</a> there are two school of thoughts and they do produce different types of designs.</p>\n\n<p>If you want to buy into the mockist approach then I highly suggest you start by looking at the <a href="http://mockobjects.com/" rel="noreferrer">mockobjects site</a> and their article <a href="http://www.mockobjects.com/book/index.html" rel="noreferrer">Mock Roles, Not Objects</a>. They also have a <a href="http://www.mockobjects.com/book/index.html" rel="noreferrer">book</a> coming out.</p>\n\n<p>The truth is even if you do believe that the mock-first style of design is not for you, or that you don\'t want to do it right through your application (e.g. not when testing your domain/service layer) then you will still want to use test doubles. <a href="http://xunitpatterns.com/" rel="noreferrer">xUnit Test Patterns</a> explains the different types of test doubles and their purposes.</p>\n\n<p>Personally I never mock domain classes, so never <a href="http://jbrains.ca/permalink/90" rel="noreferrer">mocking entities/value objects</a>. However I\'ve been trying out the mock objects style approach a lot recently, it does change my designs a bit and I find the working style comfortable. Working in this style in an MVC app I\'ll probably start with an automated acceptance test, then I\'ll write a controller test that mocks out any non-domain objects (e.g. repositories/services), then I\'ll move down to testing those repositories/services again mocking out their dependencies. I stop when I reach a class with no troublesome dependencies such as a domain entities/value object. I could go on and test against specific role interfaces which are then implemented by my domain classes, which is what the mockobjects guys would recommend, but I don\'t currently see a lot of value in that approach.</p>\n\n<p>Obviously it is worth adding that design for test is important here, but remember that although 90% of IoC/mocking/DIP examples show <a href="http://martinfowler.com/bliki/InterfaceImplementationPair.html" rel="noreferrer">interface-implementation pairs</a> (ICustomerRepository/CustomerRepository) there is a lot of value in instead looking for <a href="http://martinfowler.com/bliki/RoleInterface.html" rel="noreferrer">role interfaces</a>.</p>\n'}, {'answer_id': 2094996, 'author': 'J. B. Rainsberger', 'author_id': 253921, 'author_profile': 'https://Stackoverflow.com/users/253921', 'pm_score': 1, 'selected': False, 'text': '<p>When using services, whether my own or third party, I design to interfaces as a matter of course, so I tend to focus on the interactions there. This encourages me to design minimal interfaces.</p>\n\n<p>I check state on anything focusing on value objects, just as simple calculations, lookups, and the like.</p>\n\n<p>The next time you find yourself designing something that typically follows Model/View/Controller-or-Presenter, I highly recommend trying the Presenter First approach (Google it) using interfaces for the Model and View. This will give you a great feel for how to use stubs/mocks effectively.</p>\n'}]
['https://Stackoverflow.com/questions/54943', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4872/']