jeudi 15 juin 2017

What alternatives are there to template or link seam dependency injection for testing non-virtual methods?

I am trying to test code that has many dependencies on code which cannot be changed and often does not use virtual methods. It is also a high-performance scenario, so I can imagine some places in our own code that we'd not want tot use virtual methods. The non-virtual methods are important to the test scenarios, so I wish to mock them.

As I understand it, there are two main options:

  1. Template dependency injection: Google calls this hi-perf dependency injection. The mock is no longer a derived class of the dependency, which is replaced with a template class. It is instantiated as the original dependency's class for production and the mock class for test.

  2. Link seams: name the test class the same as the production class and use linker tricks to substitute it for the production implementation when linking tests.

I've used 1. successfully but it can quickly get out of control: I will be templatizing most of the code base to deal with the dependencies' non-virtual methods. 2. seems rather inelegant and - perhaps more importantly - will be even more unfamiliar to most people.

Are there alternative approaches? What do people do when depending on a large library of non-virtual code that they don't control?

Aucun commentaire:

Enregistrer un commentaire