Posted by Zhanyong Wan, Software EngineerFive months ago we open-sourced Google C++ Testing Framework to help C++ developers write better tests. Enthusiastic users have embraced it and sent in numerous encouraging comments and suggestions, as well as patches to make it more useful. It was a truly gratifying experience for us. Today, we are excited to release Google C++ Mocking Framework (Google Mock for short) under the new BSD license. When used with Google Test, it lets you easily create and use mock objects in C++ tests and rapid prototypes. If you aren't sure what mocks are or why you'll need them, our Why Google Mock? article will help explain why this is so exciting, and the Testing on the Toilet episode posted nearby on this blog gives a more light-hearted overview. In short, this technique can greatly improve the design and testability of software systems, as shown in this OOPSLA paper. We are happily using Google Mock in more than 100 projects at Google. It works on Linux, Windows, and Mac OS X. Its benefits include: Simple, declarative syntax for defining mocks Rich set of matchers for validating function arguments Intuitive syntax for controlling the behavior of a mock Automatic verification of expectations Easy extensibility through new user-defined matchers and actions Our users inside Google have appreciated that Google Mock is easy and even fun to use, and is an effective tool for improving software quality. We hope you'll like it too. Interested? Please take a few minutes to read the documentation and download Google Mock. Be warned, though: mocking is addictive, so proceed at your own risk. And... we'd love to hear from you! If you have any questions or feedback, please meet us on the Google Mock Discussion Group. Happy mocking! Posted by Zhanyong Wan, Software EngineerFive months ago we open-sourced Google C++ Testing Framework to help C++ developers write better tests. Enthusiastic users have embraced it and sent in numerous encouraging comments and suggestions, as well as patches to make it more useful. It was a truly gratifying experience for us. Today, we are excited to release Google C++ Mocking Framework (Google Mock for short) under the new BSD license. When used with Google Test, it lets you easily create and use mock objects in C++ tests and rapid prototypes. If you aren't sure what mocks are or why you'll need them, our Why Google Mock? article will help explain why this is so exciting, and the Testing on the Toilet episode posted nearby on this blog gives a more light-hearted overview. In short, this technique can greatly improve the design and testability of software systems, as shown in this OOPSLA paper. We are happily using Google Mock in more than 100 projects at Google. It works on Linux, Windows, and Mac OS X. Its benefits include: Simple, declarative syntax for defining mocks Rich set of matchers for validating function arguments Intuitive syntax for controlling the behavior of a mock Automatic verification of expectations Easy extensibility through new user-defined matchers and actions Our users inside Google have appreciated that Google Mock is easy and even fun to use, and is an effective tool for improving software quality. We hope you'll like it too. Interested? Please take a few minutes to read the documentation and download Google Mock. Be warned, though: mocking is addictive, so proceed at your own risk. And... we'd love to hear from you! If you have any questions or feedback, please meet us on the Google Mock Discussion Group. Happy mocking!
Posted by Zhanyong Wan, Software Engineer
Life is unfair. You work every bit as hard as Joe the Java programmer next to you. Yet as a C++ programmer, you don't get to play with all the fancy programming tools Joe takes for granted.
In particular, without a good mocking framework, mock objects in C++ have to be rolled by hand. Boy, is that tedious! (Not to mention how error-prone it is.) Why should you endure this?
Dread no more. Google Mock is finally here to help! It's a Google-originated open-source framework for creating and using C++ mocks. Inspired by jMock and EasyMock, Google Mock is easy to use, yet flexible and extensible. All you need to get started is the ability to count from 0 to 10 and use an editor.
Think you can do it? Let's try this simple example: you have a ShoppingCart class that gets the tax rate from a server, and you want to test that it remembers to disconnect from the server even when the server has generated an error. It's easy to write the test using a mock tax server, which implements this interface:
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; };
Here's how you mock it and use the mock server to verify the expected behavior of ShoppingCart:
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
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.
Create an instance of the mock class. It will be used where you would normally use a real object.
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.
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.
When the mock object is destroyed, it checks that all expectations on it have been satisfied.
You can also use Google Mock for rapid prototyping – and get a better design. To find out more, visit the project homepage at http://code.google.com/p/googlemock/. Now, be the first one on your block to use Google Mock and prepare to be envied. Did I say life is unfair?