Facilitating the spread of knowledge and innovation in professional software development



Choose your language

InfoQ Homepage News Google Has Open-Sourced Their C++ Mocking Framework

Google Has Open-Sourced Their C++ Mocking Framework

Leia em Português

This item in japanese


After open-sourcing their C++ Test Framework a few months ago, Google has just open-sourced the Google C++ Mocking Framework (Google Mock) under the BSD license.

Google Mock is used in over 100 projects inside Google and it is inspired by jMock and EasyMock, according to Zhanyong Wan, a Software Engineer at Google. The framework can be used on Linux, Windows or Mac OS X, and it addresses C++ developers. Zhanyong offers a mocking example:

class TaxServer {   // Returns the tax rate of a location (by postal code) or -1 on error.
  virtual double FetchTaxRate(
    const string& postal_code) = 0;
  virtual void CloseConnection() = 0;

class MockTaxServer : public TaxServer {     // #1
  MOCK_METHOD1(FetchTaxRate, double(const string&));
  MOCK_METHOD0(CloseConnection, void());
TEST(ShoppingCartTest,  StillCallsCloseIfServerErrorOccurs) {
  MockTaxServer mock_taxserver;              // #2
  EXPECT_CALL(mock_taxserver, FetchTaxRate(_)).

WillOnce(Return(-1));                   // #3
  EXPECT_CALL(mock_taxserver, CloseConnection());
  ShoppingCart cart(&mock_taxserver);        // #4
  cart.CalculateTax();  // Calls FetchTaxRate()
                        // and CloseConnection().
}                                            // #5

  1. Derive the mock class from the interface. For each virtual method, count how many arguments it has, name the result n, and define it using MOCK_METHODn, whose arguments are the name and type of the method.

  2. Create an instance of the mock class. It will be used where you would normally use a real object.

  3. Set expectations on the mock object (How will it be used? What will it do?). For example, the first EXPECT_CALL says that FetchTaxRate() will be called and will return an error. The underscore (_) is a matcher that says the argument can be anything. Google Mock has many matchers you can use to precisely specify what the argument should be like. You can also define your own matcher or use an exact value.

  4. Exercise code that uses the mock object. You'll get an error immediately if a mock method is called more times than expected or with the wrong arguments.

  5. When the mock object is destroyed, it checks that all expectations on it have been satisfied.

Useful links: Home Page. Complete documentation. Binaries download. Google Mock for Dummies is a quick introduction to Google Mock accompanied with examples and explanations. 

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p