I have a code base where many of the classes that I implement come from classes that are provided by other departments of my company. Working with these other units is often associated with a working relationship, as if they were third-party suppliers of intermediates.
I am trying to write test code without changing these base classes. However, there are problems with creating a meaningful test of objects due to the lack of interfaces:
//ACommonClass.h #include "globalthermonuclearwar.h" //which contains deep #include dependencies... #include "tictactoe.h" //...and need to exist at compile time to get into test... class Something //which may or may not inherit from another class similar to this... { public: virtual void fxn1(void); //which often calls into many other classes, similar to this //... int data1; //will be the only thing I can test against, but is often meaningless without fxn1 implemented //... };
Usually I retrieve the interface and work from there, but since it is a "third party", I cannot make these changes.
Currently, I have created a separate file that contains fake implementations for functions defined in third-party headers of the base class, with the need to know the basics, as described in the book "Working with Deprecated Code".
My plan was to continue to use these definitions and provide alternative test implementations for every third class I need:
//SomethingRequiredImplementations.cpp #include "ACommonClass.h" void CGlobalThermoNuclearWar::Simulate(void) {}; // fake this and all other required functions... // fake implementations for otherwise undefined functions in globalthermonuclearwar.h #include files... void Something::fxn1(void) { data1 = blah(); } //test specific functionality.
But before I start doing this, I was wondering if anyone had tried to provide actual objects based on code similar to mine, which would allow us to create new test classes to use instead of real third-party classes.
Please note that all the indicated code codes are written in C ++.
source share