background preloader

Mocks Aren't Stubs

Mocks Aren't Stubs
The term 'Mock Objects' has become a popular one to describe special case objects that mimic real objects for testing. Most language environments now have frameworks that make it easy to create mock objects. What's often not realized, however, is that mock objects are but one form of special case test object, one that enables a different style of testing. In this article I'll explain how mock objects work, how they encourage testing based on behavior verification, and how the community around them uses them to develop a different style of testing. I first came across the term "mock object" a few years ago in the Extreme Programming (XP) community. Since then I've run into mock objects more and more. But as often as not I see mock objects described poorly. This difference is actually two separate differences. Regular Tests I'll begin by illustrating the two styles with a simple example. These two behaviors imply a couple of tests, these look like pretty conventional JUnit tests.

http://martinfowler.com/articles/mocksArentStubs.html

Master-Slave Pattern The Master-Slave pattern is often used for multi-threaded applications in which many instances of the same problem must be solved. (Travelling Salesman Problem, for example.) The master creates and launches slaves to solve these instances in "parallel". When all of the slaves have finished, the master harvests the results. Master-Slave pattern is also used for user interfaces and servers. In both cases the master listens for commands coming either from the user or from clients. New Version Of Rhino Mocks A dynamic mock object framework for the .Net platform. It's purpose is to ease testing by allowing the developer to create mock implementations of custom objects and verify the interactions using unit testing. Rhino.Mocks is an attempt to create easier way to build and use mock objects and allow better refactoring support from the current tools. It's a hybrid approach between the pure Record/Replay of EasyMock.Net's model and NMock's expectation based model. Rhino.Mocks originated from EasyMock.Net and attempt to improve on their model to create easy to use and power mocking framework.

State Testing vs Interaction Testing Highly recommended reading: Fowler’s article Mocks aren’t Stubs. It’s a very clear characterization of the types of so-called Test Doubles (mocks, fakes, stubs, etc.) you can use to aid your unit testing needs, but also of the kinds of TDD you can do: classic (or state) TDD and mockist (or interaction) TDD. I got the feeling that the article seemed a bit biased towards mockist TDD, only to see in the “So should I be a classicist or a mockist?” section that Fowler himself is a classic TDDer

Inversion of Control Containers and the Dependency Injection pattern In the Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application. Underlying these containers is a common pattern to how they perform the wiring, a concept they refer under the very generic name of "Inversion of Control". In this article I dig into how this pattern works, under the more specific name of "Dependency Injection", and contrast it with the Service Locator alternative. The choice between them is less important than the principle of separating configuration from use. One of the entertaining things about the enterprise Java world is the huge amount of activity in building alternatives to the mainstream J2EE technologies, much of it happening in open source.

Rhino Mocks « Niraj Bhatt – Architect's Blog I am no mockist. But I had to upgrade myself on this for one of my clients. Below are the few links I found useful. Hope they will help you as well. You can download Rhino Mocks here. Visual Studio 2010: Test Driven Development Description Visual Studio 2010 brings with it several enhancements to help cut development friction and enable the developer to focus on the task at hand: writing high-quality code. In the following exercises we'll highlight several of the new features that the TDD developer can use to enhance his/her development cadence.

Patterns - Service Locator Oracle Technology Network > Java Software Downloads View All Downloads Top Downloads New Downloads What's New Rahien - Rhino Mocks In my previous post, I started to go over the go-raft implementation, after being interrupted by the need to sleep, I decided to go on with this, but I wanted to expand a bit first about the issue we discussed earlier, not checking the number of bytes read in log_entry’s Decode. Let us assume that we actually hit that issue, what would happen? The process goes like this, we try to read a value, but the Read method only return some of the information. We explicitly ignore that, and try to use the buffer anyway. Best case scenario, we are actually getting an error, so we bail early. At that point, we detect the error and truncate the file.

Manage test data for integration tests using Spring and DBunit Most enterprise applications rely on the database as the persistence mechanism. Integration tests for these applications require data in the database to run correctly. For integration tests to be repeatable, the tests should carry the test data they need with them and insert it before running the tests and delete it after the tests, since the data in the database can change with time. Mocking The File System to Improve Testability « Milan Nankov’s Blog Recently I started writing a simple application that would help me organize my music files by automatically renaming them using their tags and moving them to their designated folders. As expected the program will rely on the file system classes that the .Net Framework provides. Naturally, I wanted to cover the I/O logic with tests so that I am confident that when the software is used it will not do damage to my files and will behave according to my requirements. So I started writing tests… And here is the catch – in general it is not a good idea to have tests that are performing I/O operations like accessing files and databases.

BDD with SpecFlow, NUnit and MVC3–More View a List Of Products Following on from this post where it looked like BDD was a spectacularly inefficient way to drive out code, I’ll add a new feature to the scenario: Scenario: Display 10 products Given There are 10 products When the product controller is told to display the default view Then the view should contain a list of 10 products Compiling and running the tests provides templates for the Given and the Then methods, we get to reuse the existing When method. This reuse is one of the big daws for BDD for me (plus the readability).

Effective Unit Testing with DbUnit by Andrew Glover 01/21/2004 Introducing DbUnit Writing unit and component tests for objects with external dependencies, such as databases or other objects, can prove arduous, as those dependencies may hinder isolation.

Related: