BT

Google Has Open-Sourced Their C++ Mocking Framework

by Abel Avram on Dec 12, 2008 |

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. 

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.

Tell us what you think

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

Email me replies to any of my messages in this thread
Community comments

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT