[[["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 document provides a guide on how to test Cloud IAM applications using the Cloud IAM C++ client library alongside Google Test and the Google Test Mocking Framework.\u003c/p\u003e\n"],["\u003cp\u003eThe latest version of the Cloud IAM Credentials documentation available is \u003ccode\u003e2.37.0-rc\u003c/code\u003e, while the page you are viewing is referencing version \u003ccode\u003e2.22.1\u003c/code\u003e and contains a list of available documentation version numbers ranging from \u003ccode\u003e2.11.0\u003c/code\u003e to \u003ccode\u003e2.37.0-rc\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe guide demonstrates how to mock a successful \u003ccode\u003eIAMCredentialsClient::SignJwt()\u003c/code\u003e call, including setting up expectations and verifying results using the mocking framework.\u003c/p\u003e\n"],["\u003cp\u003eThe full code example provided showcases how to implement a mock object for \u003ccode\u003eIAMCredentialsConnection\u003c/code\u003e, set up expectations for the \u003ccode\u003eSignJwt\u003c/code\u003e method, and test the \u003ccode\u003eIAMCredentialsClient\u003c/code\u003e accordingly.\u003c/p\u003e\n"],["\u003cp\u003eThe page includes links to the documentation of each of these available versions, and provides a detailed walkthrough of the process and steps to take, along with the required code.\u003c/p\u003e\n"]]],[],null,["Version 2.22.1keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/iam/latest/iam-credentials-mock)\n- [2.41.0](/cpp/docs/reference/iam/2.41.0/iam-credentials-mock)\n- [2.40.0](/cpp/docs/reference/iam/2.40.0/iam-credentials-mock)\n- [2.39.0](/cpp/docs/reference/iam/2.39.0/iam-credentials-mock)\n- [2.38.0](/cpp/docs/reference/iam/2.38.0/iam-credentials-mock)\n- [2.37.0](/cpp/docs/reference/iam/2.37.0/iam-credentials-mock)\n- [2.36.0](/cpp/docs/reference/iam/2.36.0/iam-credentials-mock)\n- [2.35.0](/cpp/docs/reference/iam/2.35.0/iam-credentials-mock)\n- [2.34.0](/cpp/docs/reference/iam/2.34.0/iam-credentials-mock)\n- [2.33.0](/cpp/docs/reference/iam/2.33.0/iam-credentials-mock)\n- [2.32.0](/cpp/docs/reference/iam/2.32.0/iam-credentials-mock)\n- [2.31.0](/cpp/docs/reference/iam/2.31.0/iam-credentials-mock)\n- [2.30.0](/cpp/docs/reference/iam/2.30.0/iam-credentials-mock)\n- [2.29.0](/cpp/docs/reference/iam/2.29.0/iam-credentials-mock)\n- [2.28.0](/cpp/docs/reference/iam/2.28.0/iam-credentials-mock)\n- [2.27.0](/cpp/docs/reference/iam/2.27.0/iam-credentials-mock)\n- [2.26.0](/cpp/docs/reference/iam/2.26.0/iam-credentials-mock)\n- [2.25.1](/cpp/docs/reference/iam/2.25.1/iam-credentials-mock)\n- [2.24.0](/cpp/docs/reference/iam/2.24.0/iam-credentials-mock)\n- [2.23.0](/cpp/docs/reference/iam/2.23.0/iam-credentials-mock)\n- [2.22.1](/cpp/docs/reference/iam/2.22.1/iam-credentials-mock)\n- [2.21.0](/cpp/docs/reference/iam/2.21.0/iam-credentials-mock)\n- [2.20.0](/cpp/docs/reference/iam/2.20.0/iam-credentials-mock)\n- [2.19.0](/cpp/docs/reference/iam/2.19.0/iam-credentials-mock)\n- [2.18.0](/cpp/docs/reference/iam/2.18.0/iam-credentials-mock)\n- [2.17.0](/cpp/docs/reference/iam/2.17.0/iam-credentials-mock)\n- [2.16.0](/cpp/docs/reference/iam/2.16.0/iam-credentials-mock)\n- [2.15.1](/cpp/docs/reference/iam/2.15.1/iam-credentials-mock)\n- [2.14.0](/cpp/docs/reference/iam/2.14.0/iam-credentials-mock)\n- [2.13.0](/cpp/docs/reference/iam/2.13.0/iam-credentials-mock)\n- [2.12.0](/cpp/docs/reference/iam/2.12.0/iam-credentials-mock)\n- [2.11.0](/cpp/docs/reference/iam/2.11.0/iam-credentials-mock) \n\nTesting your Cloud IAM Credentials application with googlemock\n==============================================================\n\nThis document describes how to test your own Cloud IAM application using the Cloud IAM 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 IAMCredentialsClient::SignJwt() call\n\nFirst include the headers for the Cloud IAM Credentials Client, the mocking class, and the Google Mock framework: \n\n #include \"google/cloud/iam/credentials/v1/iam_credentials_client.h\"\n #include \"google/cloud/iam/credentials/v1/mocks/mock_iam_credentials_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::iam_credentials_v1_mocks::MockIAMCredentialsConnection;\n namespace iam = ::google::cloud::iam_credentials_v1;\n\nCreate a mocking object for [`google::cloud::iam_credentials_v1::IAMCredentialsConnection`](/cpp/docs/reference/iam/2.22.1/classgoogle_1_1cloud_1_1iam__credentials__v1_1_1IAMCredentialsConnection): \n\n auto mock = std::make_shared\u003cMockIAMCredentialsConnection\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, SignJwt)\n .WillOnce(\n [&](google::iam::credentials::v1::SignJwtRequest const& request) {\n EXPECT_EQ(\"projects/-/serviceAccounts/test-account-unique-id\",\n request.name());\n google::iam::credentials::v1::SignJwtResponse response;\n response.set_key_id(\"test-key-id\");\n return google::cloud::StatusOr\u003c\n google::iam::credentials::v1::SignJwtResponse\u003e(response);\n });\n\nWith the expectations in place, create a [`google::cloud::iam_credentials_v1::IAMCredentialsClient`](/cpp/docs/reference/iam/2.22.1/classgoogle_1_1cloud_1_1iam__credentials__v1_1_1IAMCredentialsClient) object: \n\n iam::IAMCredentialsClient iam_credentials_client(mock);\n\nAnd then make calls on the client as usual: \n\n std::string payload;\n auto response = iam_credentials_client.SignJwt(\n \"projects/-/serviceAccounts/test-account-unique-id\", {}, payload);\n\nAnd then verify the results meet your expectations: \n\n EXPECT_TRUE(response.ok());\n EXPECT_EQ(\"test-key-id\", response-\u003ekey_id());\n\n#### Full Listing\n\nFinally we present the full code for this example: \n\n\n #include \"google/cloud/iam/credentials/v1/iam_credentials_client.h\"\n #include \"google/cloud/iam/credentials/v1/mocks/mock_iam_credentials_connection.h\"\n #include \u003cgmock/gmock.h\u003e\n\n namespace {\n\n using ::google::cloud::iam_credentials_v1_mocks::MockIAMCredentialsConnection;\n namespace iam = ::google::cloud::iam_credentials_v1;\n\n TEST(MockSignJwtExample, SignJwt) {\n auto mock = std::make_shared\u003cMockIAMCredentialsConnection\u003e();\n\n EXPECT_CALL(*mock, SignJwt)\n .WillOnce(\n [&](google::iam::credentials::v1::SignJwtRequest const& request) {\n EXPECT_EQ(\"projects/-/serviceAccounts/test-account-unique-id\",\n request.name());\n google::iam::credentials::v1::SignJwtResponse response;\n response.set_key_id(\"test-key-id\");\n return google::cloud::StatusOr\u003c\n google::iam::credentials::v1::SignJwtResponse\u003e(response);\n });\n\n iam::IAMCredentialsClient iam_credentials_client(mock);\n\n std::string payload;\n auto response = iam_credentials_client.SignJwt(\n \"projects/-/serviceAccounts/test-account-unique-id\", {}, payload);\n\n EXPECT_TRUE(response.ok());\n EXPECT_EQ(\"test-key-id\", response-\u003ekey_id());\n }\n\n } // namespace"]]