How to mock an external dependency without common interface for unit test in C++

38 Views Asked by At

I need to mock an external dependency for unit test since it is not deterministic.

The dependency class cannot be abstracted out with an interface and dependency-injected into the constructor since it is in a 3rd party library. Another reason it can't use a common interface for inheritance is that the functions I need to call from this dependency are template functions.

The other option is to do template-based dependency injection into my system under test. Unfortunately, this means that my system under test class is a template class and requires all the code to be in the header. Other than toy examples, it is not tractable to have my SUT class which contains domain logic and complex code to reside entirely in a header. It seems template-based dependency injection methods require the SUT to be entirely header-based which is a severe limitation. Am I wrong here?

I have tried using template injection for my unit test but I can't get away from my SUT class not being a template class.

What are my options?

0

There are 0 best solutions below