title: Mock in the Middle with C++/Gmock
body: <p>I have a class A that instantiates an object of class B in its constructor. I want to test A with a mock object of B.</p>
  
  <p>And no, I can not pass object of B as a parameter. Is there any other way?</p>
  
  <p>I saw an article <a href="http://www.ibm.com/developerworks/library/j-mocktest/index.html" rel="nofollow">http://www.ibm.com/developerworks/library/j-mocktest/index.html</a>, with "Mock in the Middle" as the interesting topic, but that is in Java. Is it possible in C++?</p>
  
  <pre><code>class B {...};
  
  class A {
    private:
      B* b;
  
    public:
      A() {
          b = new B();
      }
      ~A() {..}
  };
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>In general, the object may be created in some other method, on demand. For example,</p>
  
  <pre><code>class A {
      ...
      int doSomething() {
          // Create an object of class B
          b = new B();
      }
  }; 
  </code></pre>