Sunday, September 14, 2014

Extreme Mocking

Recently I have been told about an aspect of unit testing using mock objects taken to an extreme I have never seen before.  I am a huge proponent of Test Driven Development and Unit Testing.  The first task I did when I started as a software engineer was to write unit tests for existing code on a project to increase code coverage. In my opinion some tests at the class level are useful while others being less strict about boxing in the code interacted have their merits as well.  This new type of mocking I have been exposed to take testing at the class level to a new extreme, all objects interacted with including the POJOs (Plain Old Java Objects) you pass as arguments should be mocked.

This is an extreme because you are not even trusting yourself.  Think about it all the objects you use to interact with your code need to be mock objects unless they are primitives.  So if you have an objects with type properties you need to create a mock object for it and its getters and setters.  In essence you are not trusting any part of the code even the most basic and everything is considered suspect.

Extreme mocking seem like it is going to far until you get into a situation where you are responsible for a code base helpless subject to the whims of other libraries.  For example you are workings on a system where one team owns the models that feed are used to interface with your system, several other teams are responsible for the data that your code interacts with, and a host of other teams interact act with your code your stuck in the middle and easy to blame.  To protect yourself you mock EVERYTHING to help triage, cover your rear-end, and insulate you from volatile systems.  The idea of not owning the models and objects that your code base is using for foreign to me as a full stack developer until I encountered it but now this idea makes sense in the situations where it is warranted. 

Try it yourself take a bit of code and mock everything it interacts with besides primitive. It is an interesting challenge.  

1 comment: