27 September 2010

Mocking real outside dependencies

In a project a while back I realized something about mocking and unit testing. In that project I tried mocking every dependency a object could have. The result was that I could write unit-tests for those objects really fast, and that I could verfy their logic very easily. That was all in the spirit of unit testing.
But something kept me nagging, I though that I wasn't getting enough values from my unit tests. Then I started doing functional tests on my services. Nothing was mocked, since the goal was to test the entire stack. Soon as I wrote a couple of tests defects in my code bellow started poping up. I fixed them and felt really good.
I though back what started me thinking that mocking everything was such a good idea. Well I worked on project where my front end integrated with a web services middle tier and I spend countless hours overtime detecting problems that originated from those services while my code worked fine. I wanted to isolate that big  outside dependency so that horrible story would not repeat again.
True to my self I just went to the extreme side and mocked everything. Now, if i change the behavior of one class I will not see what other classes are also affected by the change until I go directly to those classes and change the expected behavior of my changed class mock. This is a problem I have no intention to repeat again.
My new golden rule is : "Nikola, mock only  real outside dependencies like web services, databases and the like not your core system objects. It will make you happy."

No comments:

Post a Comment