title: Mocking non-virtual methods in C++ without editing production code?
body: <p>I am a fairly new software developer currently working adding unit tests to an existing C++ project that started years ago. Due to a non-technical reason, I'm not allowed to modify any existing code. The base class of all my modules has a bunch of methods for Setting/Getting data and communicating with other modules. </p>
  
  <p>Since I just want to unit testing each individual module, I want to be able to use canned values for all my inter-module communication methods. I.e. for a method Ping() which checks if another module is active, I want to have it return true or false based on what kind of test I'm doing. I've been looking into Google Test and Google Mock, and it does support mocking non-virtual methods. However the approach described (<a href="http://code.google.com/p/googlemock/wiki/CookBook#Mocking_Nonvirtual_Methods" rel="noreferrer">http://code.google.com/p/googlemock/wiki/CookBook#Mocking_Nonvirtual_Methods</a>) requires me to "templatize" the original methods to take in either real or mock objects. I can't go and templatize my methods in the base class due to the requirement mentioned earlier, so I need some other way of mocking these virtual methods </p>
  
  <p>Basically, the methods I want to mock are in some base class, the modules I want to unit test and create mocks of are derived classes of that base class. There are intermediate modules in between my base Module class and the modules that I want to test.</p>
  
  <p>I would appreciate any advise!</p>
  
  <p>Thanks, </p>
  
  <p>JW</p>
  
  <p><strong>EDIT: A more concrete examples</strong></p>
  
  <p>My base class is lets say rootModule, the module I want to test is leafModule. There is an intermediate module which inherits from rootModule, leafModule inherits from this intermediate module.</p>
  
  <p>In my leafModule, I want to test the doStuff() method, which calls the non virtual GetStatus(moduleName) defined in the rootModule class. I need to somehow make GetStatus() to return a chosen canned value. Mocking is new to me, so is using mock objects even the right approach?</p>