title: Re-routing a real function to a mocking function with Gmock?
body: <p>When I researched Gmock from Google, I've installed and built the project work well so far. But I have some concern about mocking a function. Now I have the following files:</p>
  
  <p>&lt;1> myGtest.h</p>
  
  <pre><code>#ifndef MYGTEST_H_
  #define MYGTEST_H_
  
  int test(int);
  int function(int);
  
  #endif /* MYGTEST_H_ */
  </code></pre>
  
  <p>&lt;2> src_code.cpp</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "myGtest.h"
  
  int test(int a) {
      printf("NOT overridden!\n");
      return a;
  }
  
  int function(int a){
  
      int x = test(a);
      if(x == 0)
          return 99;
      else
          return 0;
  }
  </code></pre>
  
  <p>&lt;3> myGtest_dummy.h</p>
  
  <pre><code>#ifndef MYGTEST_DUMMY_H_
  #define MYGTEST_DUMMY_H_
  
  #include "gmock/gmock.h"
  #include "../myGtest/myGtest.h"
  
  class myGtestMock
  {
  public:
      myGtestMock(){};
      ~myGtestMock(){};
      MOCK_METHOD1(test, int(int));
  };
  
  #endif /* MYGTEST_DUMMY_H_ */
  </code></pre>
  
  <p>&lt;4> test_program.cpp</p>
  
  <pre><code>#include &lt;gmock/gmock.h&gt;
  #include &lt;gtest/gtest.h&gt;
  #include "src/myGtest/myGtest.h"
  #include "src/dummy/myGtest_dummy.h"
  
  using testing::_;
  using testing::Return;
  using testing::InSequence;
  using ::testing::AtLeast;
  
  extern int function(int a);
  extern int test(int a);
  
  class BTest:public testing::Test{
  public:
      myGtestMock mock_test;
  
  
      int __wrap_test(int a);
  };
  
  int BTest::__wrap_test(int a){
  
      printf("overridden!\n");
      return a;
  }
  
  TEST_F(BTest, CallMockTest) {
  
      EXPECT_CALL(mock_test, test(0))
              .WillOnce(Invoke(this, &amp;BTest::__wrap_test));
  
      function(99);
  }
  
  int main(int argc, char *argv[]) {
  
      ::testing::InitGoogleTest(&amp;argc, argv);
      return RUN_ALL_TESTS();
  }
  </code></pre>
  
  <p>Could you help me explain that: How can I mock the function <code>int test(int)</code>? I'd like to expect that once <code>TEST_F(BTest, CallMockTest)</code> is executed, the program call <code>function(99);</code>.  Then my mocking function <code>int __wrap_test(int)</code> will be called instead of <code>int test(int)</code>.</p>
  
  <p>Thanks so much for the answer.</p>