vendredi 30 octobre 2015

Is faking dependencies with interfaces or virtual methods a good design choice?

I am trying to learn unit testing but there is a design issue resulting from it. Consider class A has a dependency on class B. If you want to create a stub for B in order to unit test A, most isolation frameworks require that B has to be an interface or all the methods used by A must be virtual. B can't be a concrete class with non-virtual methods in essence in order to unit test.

This imposes major restrictions on the design of production code. If I have to create an interface for every dependency then number of classes will double. Also I create interfaces for volatile dependencies(likely to change in the future) or if the design requires it for extensibility. Polluting the production code with interfaces just for testing will increase its complexity significantly. Making all methods virtual doesn't seem to be a good solution also. It gives the inheritors impression that these methods are ok to be overridden even if they aren't and this is just a side effect of unit testing.

Does this mean that testable object oriented design doesn't allow concrete dependencies or does it mean that concrete dependencies shouldn't be faked? "Every dependency must be faked(stub or mock) to unit test correctly" is what I learned so far so I don't think latter is the case. Isolation frameworks other than JustMock and Isolator doesn't allow faking concrete dependencies without virtual methods and some argue that power of JustMock and Isolator lead to bad design. I think that the ability to mock any class is very powerful and it will keep the design of production code clean if you know what you are doing.

Aucun commentaire:

Enregistrer un commentaire