I got some hands-on experience unit testing with mock objects recently. I must admid I'm very positive about it.
The project I'm working on is getting bigger and it is becoming harder to set up the environment in order to test a small piece of functionality. This is a problem that comes with the so called "state testing" that we were using. With mock objects an other way of testing known as "interaction testing" is becoming possible. This way you don't have to set up an entire environment to test a small piece of functionality.
With state testing an initial application state is set up, the function under test is called and then the resulting application state is checked. The environment I'm working with is largely stored in a database that has to be brought in an initial state at the beginning of every test. There are several ways to do this but you'll always need an ever growing set of test data. It's this set of test data that is becoming harder to maintain every day new functionality is added.
When unittesting with mock objects "interaction testing" is becoming available. With this kind of testing the function under test is isolated from the rest of the system. All surrounding objects are replaced by mock objects wich have expectations about the methods that should be called on them by the primary function.
Each mock object checks if these expectations are met when the primary method is kickstarted. This can go as far as checking if each parameter for the expected method call has the right value.
I find that it's easier to write my test first when using interaction testing. It forces me to think about the methods that I'll need for my functionality to work. One step closer to test driven design! The big advantage of interaction testing is that there is no longer a test set to maintain. A disadvantage that sould be noted is that it is getting a little harder to do code refactoring. When object interaction changes, the test code also has to change.
Martin Fowler wrote an excellent article on the subject called Mocks Aren't Stubs In wich he also describes the difference between state based testing and interaction based testing.