Skip to content

Feature: Wrap GoogleMock (gmock) in ut-core with UT_ mocking macros + examples #253

Description

@Ulrond

Problem/Opportunity

ut-core wraps the gtest backend (include/ut_gtest.h, src/cpp_source/ut_gtest.cpp) with UT_ASSERT_* / UT_ADD_TEST macros, but it does not wrap GoogleMock (gmock). There is currently no supported way to author mocks for a C++ interface under test, which is the core need when unit-testing abstract HAL/AIDL C++ interfaces.

The pinned googletest release (1.15.2, fetched by build.sh into framework/gtest/<target>/googletest-1.15.2) is the combined distribution — it already contains googlemock/ alongside googletest/. So gmock source is present; it is simply not built, linked, initialised, or wrapped.

Proposed Solution

Wrap gmock in ut-core, mirroring the existing gtest wrap:

  1. Build — build the gmock libraries from the already-fetched googletest tree (build.sh): produce libgmock.a / libgmock_main.a into build/<target>/cpp_libs alongside the gtest libs (gmock's CMake builds both).
  2. Link + include — in the Makefile GTEST case, add googlemock/include to INC_DIRS and -lgmock to the link line (before -lgtest).
  3. Initialise — call ::testing::InitGoogleMock(&argc, argv) in the UTTestRunner constructor (instead of / in addition to InitGoogleTest), so gmock installs its verification listener and unmet EXPECT_CALLs fail the run.
  4. Wrapper header — add include/ut_gmock.h exposing ut-core-styled macros over gmock: UT_MOCK_METHOD, UT_EXPECT_CALL, and thin aliases for the common matchers/actions (UT_ANY/_, UT_RETURN, UT_RETURN_REF, UT_INVOKE, UT_TIMES, UT_AT_LEAST, ...). Pull it in from ut_gtest.h (or ut.h) for the CPP variant.
  5. Examples — add mocking example tests under tests/src/cpp_source/ (e.g. ut_test_gmock.cpp): a small abstract interface, a UT_MOCK_METHOD mock, and tests demonstrating expectation setting, matchers, actions (Return/Invoke), cardinality, and a deliberately-unmet expectation (documented/negative case).
  6. Docs — update AGENTS.md (§ gtest wrap → add gmock), README.md, and the template/ test example to show mock usage.

Acceptance Criteria

  • A user can define a mock of a C++ interface using UT_MOCK_METHOD, set expectations with UT_EXPECT_CALL, and use common matchers/actions — all via UT_-prefixed macros, no direct gmock/gmock.h include required in the test.
  • Unmet expectations fail the test run (gmock verification is active).
  • The mocking example suite builds and runs under the CPP/gtest variant (./build.sh TARGET=linux VARIANT=CPP or equivalent) and is registered like other suites.
  • No regression to the existing gtest wrap or the C/CUnit variant.

Additional Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions