mardi 23 décembre 2014

UnitTesting: Is "Subclass and override" technique considered to be a "bad practice"?

Sometimes when you deal with legacy code you find it hard to break the dependencies. One of the solutions that some unit-testing gurus (e.g. Roy Osherove in The Art of Unit Testing or Michael Feathers in Working Effectively with Legacy Code) propose is to use the "Subclass and override" technique. The idea of this technique is to encapsulate the usage of dependency in protected method and then test the extended class with that method overridden with method that uses fake dependency.


Also there are two more techniques that, imo, are doing almost the same. Those are:



  • mock the protected method that contains dependency

  • use reflection (if it is supported in your language) to make the dependency-containing protected method to return what is needed


However the latter two techniques are considered anti-patterns (you don't mock what you are testing). But, to me, all three techniques are using the same idea. Is the "Subclass and override" technique also considered an anti-pattern and if not, why?


Aucun commentaire:

Enregistrer un commentaire