How do I mock non-overridden non virtual/virtual methods in a base class and test just the derived class’s methods?
The case here is:
I have a base class X which has methods that connect to an external server and do a couple of other things.
I have a class Y derived from X. I have implemented two methods in Y. I want to just unit test them. I am worried only about these two methods and I don’t want the base class implementation to be called to connect to the server etc( I want to mock those methods out , but i don’t want to override those methods in my derived class Y and do nothing in them, since it is production code).
Any thoughts /ideas on how can I unit test my those methods in isolation?
P.S: I am using C++/GTest for development and unit testing.
One option is to create a Mock_base class and the class Derived inheriting from it in the test directory. Now mock out any implementations in the actual Base that you are not interested in with functions that do nothing. Example the Mock_base::Connect() may return SUCCESS return code. This way you can unit test the Derived:funcs() without modifying the source code.