Testing your Cloud BigQueryRead application with googlemock
This document describes how to test your own Cloud BigQueryRead application using the Cloud BigQueryRead C++ client library, Google Test and the Google Test Mocking Framework.
Mocking a Successful BigQueryReadClient::GetServiceAccount() call
First include the headers for the Cloud BigQueryRead 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"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-14 UTC."],[[["\u003cp\u003eThis page provides guidance on testing Cloud BigQueryRead applications using the C++ client library, Google Test, and the Google Test Mocking Framework.\u003c/p\u003e\n"],["\u003cp\u003eThe document includes examples of mocking \u003ccode\u003eBigQueryReadClient\u003c/code\u003e calls, specifically demonstrating how to mock a successful \u003ccode\u003eCreateReadSession\u003c/code\u003e call.\u003c/p\u003e\n"],["\u003cp\u003eThe content demonstrates the use of \u003ccode\u003eMockBigQueryReadConnection\u003c/code\u003e for creating a mock object to setup expectations and verify results.\u003c/p\u003e\n"],["\u003cp\u003eThe full code example illustrates the complete process of mocking, including setting up expectations, creating a client, making calls, and validating the results, making it easy to apply in one's project.\u003c/p\u003e\n"],["\u003cp\u003eThe latest version of the mock is 2.37.0-rc, while version 2.27.0 is covered in detail on the page.\u003c/p\u003e\n"]]],[],null,["Version 2.27.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/bigquery/latest/bigquery-read-mock)\n- [2.41.0](/cpp/docs/reference/bigquery/2.41.0/bigquery-read-mock)\n- [2.40.0](/cpp/docs/reference/bigquery/2.40.0/bigquery-read-mock)\n- [2.39.0](/cpp/docs/reference/bigquery/2.39.0/bigquery-read-mock)\n- [2.38.0](/cpp/docs/reference/bigquery/2.38.0/bigquery-read-mock)\n- [2.37.0](/cpp/docs/reference/bigquery/2.37.0/bigquery-read-mock)\n- [2.36.0](/cpp/docs/reference/bigquery/2.36.0/bigquery-read-mock)\n- [2.35.0](/cpp/docs/reference/bigquery/2.35.0/bigquery-read-mock)\n- [2.34.0](/cpp/docs/reference/bigquery/2.34.0/bigquery-read-mock)\n- [2.33.0](/cpp/docs/reference/bigquery/2.33.0/bigquery-read-mock)\n- [2.32.0](/cpp/docs/reference/bigquery/2.32.0/bigquery-read-mock)\n- [2.31.0](/cpp/docs/reference/bigquery/2.31.0/bigquery-read-mock)\n- [2.30.0](/cpp/docs/reference/bigquery/2.30.0/bigquery-read-mock)\n- [2.29.0](/cpp/docs/reference/bigquery/2.29.0/bigquery-read-mock)\n- [2.28.0](/cpp/docs/reference/bigquery/2.28.0/bigquery-read-mock)\n- [2.27.0](/cpp/docs/reference/bigquery/2.27.0/bigquery-read-mock)\n- [2.26.0](/cpp/docs/reference/bigquery/2.26.0/bigquery-read-mock)\n- [2.25.1](/cpp/docs/reference/bigquery/2.25.1/bigquery-read-mock)\n- [2.24.0](/cpp/docs/reference/bigquery/2.24.0/bigquery-read-mock)\n- [2.23.0](/cpp/docs/reference/bigquery/2.23.0/bigquery-read-mock)\n- [2.22.1](/cpp/docs/reference/bigquery/2.22.1/bigquery-read-mock)\n- [2.21.0](/cpp/docs/reference/bigquery/2.21.0/bigquery-read-mock)\n- [2.20.0](/cpp/docs/reference/bigquery/2.20.0/bigquery-read-mock)\n- [2.19.0](/cpp/docs/reference/bigquery/2.19.0/bigquery-read-mock)\n- [2.18.0](/cpp/docs/reference/bigquery/2.18.0/bigquery-read-mock)\n- [2.17.0](/cpp/docs/reference/bigquery/2.17.0/bigquery-read-mock)\n- [2.16.0](/cpp/docs/reference/bigquery/2.16.0/bigquery-read-mock)\n- [2.15.1](/cpp/docs/reference/bigquery/2.15.1/bigquery-read-mock)\n- [2.14.0](/cpp/docs/reference/bigquery/2.14.0/bigquery-read-mock)\n- [2.13.0](/cpp/docs/reference/bigquery/2.13.0/bigquery-read-mock)\n- [2.12.0](/cpp/docs/reference/bigquery/2.12.0/bigquery-read-mock)\n- [2.11.0](/cpp/docs/reference/bigquery/2.11.0/bigquery-read-mock) \n\nTesting your Cloud BigQueryRead application with googlemock\n===========================================================\n\nThis document describes how to test your own Cloud BigQueryRead application using the Cloud BigQueryRead C++ client library, [Google Test](https://github.com/google/googletest) and the [Google Test Mocking Framework](https://github.com/google/googletest/tree/main/googlemock).\n\n#### Mocking a Successful BigQueryReadClient::GetServiceAccount() call\n\nFirst include the headers for the Cloud BigQueryRead Client, the mocking class, and the Google Mock framework: \n\n #include \"google/cloud/bigquery/storage/v1/bigquery_read_client.h\"\n #include \"google/cloud/bigquery/storage/v1/mocks/mock_bigquery_read_connection.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::bigquery_storage_v1_mocks::MockBigQueryReadConnection;\n namespace bigquery = ::google::cloud::bigquery_storage_v1;\n\nCreate a mocking object for [`google::cloud::bigquery_storage_v1::BigQueryReadConnection`](/cpp/docs/reference/bigquery/2.27.0/classgoogle_1_1cloud_1_1bigquery__storage__v1_1_1BigQueryReadConnection): \n\n auto mock = std::make_shared\u003cMockBigQueryReadConnection\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, CreateReadSession)\n .WillOnce([&](google::cloud::bigquery::storage::v1::\n CreateReadSessionRequest const& request) {\n EXPECT_EQ(\"test-project-name\", request.parent());\n EXPECT_EQ(\"test-table-name\", request.read_session().table());\n google::cloud::bigquery::storage::v1::ReadSession response;\n google::cloud::bigquery::storage::v1::ReadStream stream;\n stream.set_name(\"test-stream\");\n *response.add_streams() = stream;\n return google::cloud::StatusOr\u003c\n google::cloud::bigquery::storage::v1::ReadSession\u003e(response);\n });\n\nWith the expectations in place, create a [`google::cloud::bigquery_storage_v1::BigQueryReadClient`](/cpp/docs/reference/bigquery/2.27.0/classgoogle_1_1cloud_1_1bigquery__storage__v1_1_1BigQueryReadClient) object: \n\n bigquery::BigQueryReadClient bigquery_client(mock);\n\nAnd then make calls on the client as usual: \n\n google::cloud::bigquery::storage::v1::ReadSession read_session;\n read_session.set_table(\"test-table-name\");\n int max_streams = 1;\n auto session = bigquery_client.CreateReadSession(\"test-project-name\",\n read_session, max_streams);\n\nAnd then verify the results meet your expectations: \n\n EXPECT_TRUE(session.ok());\n EXPECT_EQ(\"test-stream\", session-\u003estreams(0).name());\n\n#### Full Listing\n\nFinally we present the full code for this example: \n\n #include \"google/cloud/bigquery/storage/v1/bigquery_read_client.h\"\n #include \"google/cloud/bigquery/storage/v1/mocks/mock_bigquery_read_connection.h\"\n #include \u003cgmock/gmock.h\u003e\n\n namespace {\n\n using ::google::cloud::bigquery_storage_v1_mocks::MockBigQueryReadConnection;\n namespace bigquery = ::google::cloud::bigquery_storage_v1;\n\n TEST(MockCreateReadSessionExample, CreateReadSession) {\n auto mock = std::make_shared\u003cMockBigQueryReadConnection\u003e();\n\n EXPECT_CALL(*mock, CreateReadSession)\n .WillOnce([&](google::cloud::bigquery::storage::v1::\n CreateReadSessionRequest const& request) {\n EXPECT_EQ(\"test-project-name\", request.parent());\n EXPECT_EQ(\"test-table-name\", request.read_session().table());\n google::cloud::bigquery::storage::v1::ReadSession response;\n google::cloud::bigquery::storage::v1::ReadStream stream;\n stream.set_name(\"test-stream\");\n *response.add_streams() = stream;\n return google::cloud::StatusOr\u003c\n google::cloud::bigquery::storage::v1::ReadSession\u003e(response);\n });\n\n bigquery::BigQueryReadClient bigquery_client(mock);\n\n google::cloud::bigquery::storage::v1::ReadSession read_session;\n read_session.set_table(\"test-table-name\");\n int max_streams = 1;\n auto session = bigquery_client.CreateReadSession(\"test-project-name\",\n read_session, max_streams);\n\n EXPECT_TRUE(session.ok());\n EXPECT_EQ(\"test-stream\", session-\u003estreams(0).name());\n }\n\n } // namespace"]]