Excellent paper on mocking framework by jmock author. This paper was written in 2004 that is 18 years ago but has many tips of building maintainable software system.
In this post i will highlight key ideas from this paper but suggest you to read the paper to get big ideas behind mocking and programming practice.
Mock objects are extension of test driven development.
Mock objects can be useful when we start thinking about writing test first as this allows to mock parts that is still not developed. Think like better way of building prototype system.
Mock object are less interesting as a technique for isolating tests from third-party libraries.
This is common misconception about mock and i have seen/written many codes using mock like this. This was really eye opening fact that comes from author of mocking framework.
Writing test is design activity
This is so much true but as engineer we take shortcut many time to throw away best part of writing test. Design that is driven from test also gives insights about real problem and it lead to
invention because developer has to think hard about problem and avoid over engineering
Coupling and cohesion
As we start wiring test it gives good idea on coupling & cohesion decision we make. Good software will have low coupling and high cohesion. This also lead to functional decomposition of task.
Another benefit of well design system is that it does not have Law_of_Demeter, this is one of the common problem that gets introduced in system unknowingly. Lots of micro services suffer from this anti pattern.
Need driven development
As mocking requires explicit code/setup, so it comes from need/demand of test case. You don’t code based on forecast that some feature will required after 6 months, so this allows to focus on need of customer. All the interfaces that is produce as result of test is narrow and fit for purpose. This type of development is also called top down development.
Quote from paper
We find that Need-Driven Development helps us stay focused on the requirements in hand and to develop coherent objects.
Programming by composition
Test first approach allows you to think about Composability of components, every thing is passed as constructor arguments or as method parameter.
Once system is build using such design principal it is very easy to test/replace part of system.
Mock objects allows to think about Composability so that some parts of system are mocked.
Mock test becomes too complicated
One observation in paper talks about complexity of Mock Test.
If system design is weak then mocking will be hard and complicated. It does amplification of problems like coupling, separation of concern. I think this is best use of mock objects to get feedback on design and use it like motivator to make system better.
Don’t add behavior to mock
As per paper we should never add behavior to stub and in case if you get the temptation to do that then it is sign of misplaced responsibility.
If you like the post then you can follow me on twitter to be notified about random stuff that i write.