Mocking framework vs mocks by hand

There was an interesting discussion on Twitter a few weeks ago on whether we should use mocking frameworks or not:

  • I asked to the author of the original post, why do you prefer hand-crafted stubs and fakes, Ted?

  • Ted M. Young’s response was: “The ease of using a tool to create them means you won’t notice how complex the real thing has become. So, if it becomes difficult or tedious to create them by hand, that often points to interfaces or classes that have become too large, with too many responsibilities.”

Ted has a good point here. If you need complex mocking, then, maybe something is more complex than it should or needs to be.

I still support the use of a mocking framework, though. It’s so much easier to use the Mockito’s two lines of code to create a stub. But you should look at all the mock code and think “I could do this by hand if I wanted to”. That’s the key.

If you liked this post, subscribe to our mailing list!