Testing your Cloud Pub/Sub publisher application with googlemock
This document describes how to test your own Cloud Pub/Sub application using the Cloud Pub/Sub C++ client library,Google Testand the andGoogle Test Mocking Framework.
Mocking a Successful Publisher::Publish() call
First include the headers for the Cloud Pub/Sub Publisher Client, the mocking class, and the Google Mock framework.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis document provides instructions on how to test a Cloud Pub/Sub application using the C++ client library, Google Test, and the Google Test Mocking Framework.\u003c/p\u003e\n"],["\u003cp\u003eThe latest release candidate version for the pub sub library, 2.37.0-rc, is available and links to documentation for past versions ranging down to version 2.11.0, which can also be accessed.\u003c/p\u003e\n"],["\u003cp\u003eThe document demonstrates mocking a successful \u003ccode\u003ePublisher::Publish()\u003c/code\u003e call using \u003ccode\u003eMockPublisherConnection\u003c/code\u003e, allowing developers to set expectations and verify results in their tests.\u003c/p\u003e\n"],["\u003cp\u003eThe key example in the document, called \u003ccode\u003eMockPublishExample, PublishSimple\u003c/code\u003e, shows how to set up expectations for a mocked \u003ccode\u003ePublisherConnection\u003c/code\u003e object and create a \u003ccode\u003ePublisher\u003c/code\u003e object from it, and provides a fully working example of the process.\u003c/p\u003e\n"],["\u003cp\u003eThe document uses aliases to improve code readability, showing an example using \u003ccode\u003eMockPublisherConnection\u003c/code\u003e as a mocking object, and the pubsub namespace.\u003c/p\u003e\n"]]],[],null,["Version 2.22.1keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/pubsub/latest/publisher-mock)\n- [2.41.0](/cpp/docs/reference/pubsub/2.41.0/publisher-mock)\n- [2.40.0](/cpp/docs/reference/pubsub/2.40.0/publisher-mock)\n- [2.39.0](/cpp/docs/reference/pubsub/2.39.0/publisher-mock)\n- [2.38.0](/cpp/docs/reference/pubsub/2.38.0/publisher-mock)\n- [2.37.0](/cpp/docs/reference/pubsub/2.37.0/publisher-mock)\n- [2.36.0](/cpp/docs/reference/pubsub/2.36.0/publisher-mock)\n- [2.35.0](/cpp/docs/reference/pubsub/2.35.0/publisher-mock)\n- [2.34.0](/cpp/docs/reference/pubsub/2.34.0/publisher-mock)\n- [2.33.0](/cpp/docs/reference/pubsub/2.33.0/publisher-mock)\n- [2.32.0](/cpp/docs/reference/pubsub/2.32.0/publisher-mock)\n- [2.31.0](/cpp/docs/reference/pubsub/2.31.0/publisher-mock)\n- [2.30.0](/cpp/docs/reference/pubsub/2.30.0/publisher-mock)\n- [2.29.0](/cpp/docs/reference/pubsub/2.29.0/publisher-mock)\n- [2.28.0](/cpp/docs/reference/pubsub/2.28.0/publisher-mock)\n- [2.27.0](/cpp/docs/reference/pubsub/2.27.0/publisher-mock)\n- [2.26.0](/cpp/docs/reference/pubsub/2.26.0/publisher-mock)\n- [2.25.1](/cpp/docs/reference/pubsub/2.25.1/publisher-mock)\n- [2.24.0](/cpp/docs/reference/pubsub/2.24.0/publisher-mock)\n- [2.23.0](/cpp/docs/reference/pubsub/2.23.0/publisher-mock)\n- [2.22.1](/cpp/docs/reference/pubsub/2.22.1/publisher-mock)\n- [2.21.0](/cpp/docs/reference/pubsub/2.21.0/publisher-mock)\n- [2.20.0](/cpp/docs/reference/pubsub/2.20.0/publisher-mock)\n- [2.19.0](/cpp/docs/reference/pubsub/2.19.0/publisher-mock)\n- [2.18.0](/cpp/docs/reference/pubsub/2.18.0/publisher-mock)\n- [2.17.0](/cpp/docs/reference/pubsub/2.17.0/publisher-mock)\n- [2.16.0](/cpp/docs/reference/pubsub/2.16.0/publisher-mock)\n- [2.15.1](/cpp/docs/reference/pubsub/2.15.1/publisher-mock)\n- [2.14.0](/cpp/docs/reference/pubsub/2.14.0/publisher-mock)\n- [2.13.0](/cpp/docs/reference/pubsub/2.13.0/publisher-mock)\n- [2.12.0](/cpp/docs/reference/pubsub/2.12.0/publisher-mock)\n- [2.11.0](/cpp/docs/reference/pubsub/2.11.0/publisher-mock) \n\nTesting your Cloud Pub/Sub publisher application with googlemock\n================================================================\n\nThis document describes how to test your own Cloud Pub/Sub application using the Cloud Pub/Sub C++ client library, [Google Test](https://github.com/google/googletest) and the and [Google Test Mocking Framework](https://github.com/google/googletest/tree/main/googlemock).\n\n#### Mocking a Successful Publisher::Publish() call\n\nFirst include the headers for the Cloud Pub/Sub Publisher Client, the mocking class, and the Google Mock framework. \n\n #include \"google/cloud/pubsub/mocks/mock_publisher_connection.h\"\n #include \"google/cloud/pubsub/publisher.h\"\n #include \u003cgmock/gmock.h\u003e\n\nThe example uses a number of aliases to save typing and improve readability: \n\n using ::google::cloud::pubsub_mocks::MockPublisherConnection;\n namespace pubsub = ::google::cloud::pubsub;\n\nCreate a mocking object for [`google::cloud::pubsub::PublisherConnection`](/cpp/docs/reference/pubsub/2.22.1/classgoogle_1_1cloud_1_1pubsub_1_1PublisherConnection): \n\n auto mock = std::make_shared\u003cMockPublisherConnection\u003e();\n\nIt is customary to first setup the expectations for your mock, and then write the rest of the code: \n\n EXPECT_CALL(*mock, Publish)\n .WillOnce([&](pubsub::PublisherConnection::PublishParams const& p) {\n EXPECT_EQ(\"test-data-0\", p.message.data());\n return google::cloud::make_ready_future(\n google::cloud::StatusOr\u003cstd::string\u003e(\"test-id-0\"));\n });\n\nWith the expectations in place, create a [`google::cloud::pubsub::Publisher`](/cpp/docs/reference/pubsub/2.22.1/classgoogle_1_1cloud_1_1pubsub_1_1Publisher) object: \n\n pubsub::Publisher publisher(mock);\n\nAnd then make calls on the client as usual: \n\n auto id =\n publisher.Publish(pubsub::MessageBuilder{}.SetData(\"test-data-0\").Build())\n .get();\n\nAnd then verify the results meet your expectations: \n\n EXPECT_TRUE(id.ok());\n EXPECT_EQ(\"test-id-0\", *id);\n\n#### Full Listing\n\nFinally we present the full code for this example: \n\n\n #include \"google/cloud/pubsub/mocks/mock_publisher_connection.h\"\n #include \"google/cloud/pubsub/publisher.h\"\n #include \u003cgmock/gmock.h\u003e\n\n namespace {\n\n using ::google::cloud::pubsub_mocks::MockPublisherConnection;\n namespace pubsub = ::google::cloud::pubsub;\n\n TEST(MockPublishExample, PublishSimple) {\n auto mock = std::make_shared\u003cMockPublisherConnection\u003e();\n\n EXPECT_CALL(*mock, Publish)\n .WillOnce([&](pubsub::PublisherConnection::PublishParams const& p) {\n EXPECT_EQ(\"test-data-0\", p.message.data());\n return google::cloud::make_ready_future(\n google::cloud::StatusOr\u003cstd::string\u003e(\"test-id-0\"));\n });\n\n pubsub::Publisher publisher(mock);\n\n auto id =\n publisher.Publish(pubsub::MessageBuilder{}.SetData(\"test-data-0\").Build())\n .get();\n\n EXPECT_TRUE(id.ok());\n EXPECT_EQ(\"test-id-0\", *id);\n }\n\n } // namespace"]]