skip to content

As a lead, what GoogleMock policy would you set for a codebase on nice versus strict mocks, ON_CALL versus EXPECT_CALL, and which types tests may mock?

level: principalimportance: should knowfreq 35%

answer

  1. nice by default, strict last
  2. stub with ON_CALL, verify sparingly
  3. mock only what you own
  4. one shared mock per interface

basics

~20 s

Default to NiceMock or naggy mocks and keep StrictMock for protocols where any extra call is a bug; stub with ON_CALL and use EXPECT_CALL only for the interaction under test; mock only interfaces the team owns, through adaptors, with one shared mock per interface.

solid answer

~40 s

I would start from GoogleMock's own guidance and make it enforceable. **Strictness**: nice mocks most of the time, naggy while developing a test, and `StrictMock` only as a last resort, because strict mocks fail on harmless refactors. Where an extra call really is a defect, say a heater command in a safety interlock, I prefer an explicit `EXPECT_CALL(heater, SetHeater(true)).Times(0)` over blanket strictness. **Verification**: `ON_CALL` for stubbing, `EXPECT_CALL` only for the behaviour a test is named after. **Ownership**: never mock a third-party class directly; wrap it in an adaptor interface we own and mock that, and keep one shared mock per interface beside it rather than ad-hoc copies. Enforce it in review and in a short testing guide, and measure it by how often refactors break tests that did not change behaviour.

go deeper

for a junior

Know that GoogleMock offers nice, naggy and strict mocks and that a team usually sets a default.

for a middle

Explain why ON_CALL for stubbing and few EXPECT_CALLs keep tests stable through refactors.

for a senior

Argue for adaptor interfaces over mocking foreign classes and identify which interfaces deserve strict treatment.

for a principal

Own the policy: pick defaults, list the strict exceptions, place shared mocks, and measure whether refactors keep breaking behaviour-preserving tests.

## The decision a lead actually owns GoogleMock gives every test author the same three dials: how strict a mock is, how many interactions to verify, and what gets mocked. Left to individual taste, a codebase ends up with both extremes: suites where every refactor breaks dozens of tests, and suites that verify nothing. There is no single right setting; the lead's job is to choose defaults, write down the exceptions, and make them cheap to follow. ## Dial 1: strictness GoogleMock's cookbook gives an explicit recommendation: use **nice mocks most of the time**, **naggy mocks** (the current default) while developing or debugging a test, and **strict mocks only as the last resort**, because naggy and strict mocks make tests brittle when code is refactored without changing externally visible behaviour. Options for the policy: | Policy | What it buys | What it costs | |---|---|---| | Nice by default | refactors rarely break tests | a new call to a method no test mentions goes unnoticed | | Naggy (do nothing) | warnings point at forgotten constraints | noisy logs that people learn to ignore | | Strict by default | any unplanned call fails | every new call to a dependency breaks unrelated tests | | Strict for chosen interfaces | protects real protocols | needs a list everyone knows | A common compromise is nice by default, plus explicit negative expectations where an extra call is genuinely harmful: `EXPECT_CALL(heater, SetHeater(true)).Times(0)` documents the rule in the test that cares. `StrictMock` is reserved for interfaces whose every call is part of the contract, such as a wire protocol or a hardware command bus. A binary-wide switch also exists, `--gmock_default_mock_behavior` (0 nice, 1 naggy, 2 strict), but flipping it for an existing suite changes the meaning of every unwrapped mock at once, so it is a migration, not a setting. ## Dial 2: how much to verify The cookbook calls `ON_CALL` "likely the single most under-utilized construct" and recommends using it by default, with `EXPECT_CALL` only when a test actually intends to verify a call. A policy can make that concrete: - shared stubbing lives in fixtures as `ON_CALL`s; - each test verifies one behaviour, named in its title, with as few `EXPECT_CALL`s as that needs; - call order is enforced with `InSequence` or `After` only when the order is part of the contract; - matchers check the fields that matter (`Field(&Command::percent, Gt(50))`) rather than whole objects. ## Dial 3: what may be mocked GoogleMock's introduction states it bluntly: generally, **don't mock classes you don't own**. When a library's owner changes its API, every hand-written mock of it breaks. The recommended alternatives: 1. introduce a thin **adaptor** interface the team owns, shaped for the domain (`ThermostatClock` rather than a vendor's full time API), and mock that; 2. if a foreign class must be mocked, keep **one shared mock** beside its interface, in one header, so a change is fixed in one place; 3. avoid making concrete classes' functions virtual just to mock them; the cookbook warns that this weakens control of the class's invariants. For the thermostat, that means mocking `Sensor` and `Actuator` interfaces the team designed, not the vendor's I2C driver class. ## Making the policy stick - Write the defaults into the testing guide with one example each. - Put shared mocks in a test-only library next to each interface, so nobody writes a second `MockSensor`. - Review for over-specification: a test with many `EXPECT_CALL`s and a generic name is a smell. - Track a signal: how often behaviour-preserving changes require test edits. Rising numbers mean the suite verifies implementation, not contract. The honest trade-off to state: a lenient policy risks missing an extra call the business cared about; a strict one taxes every change. The policy should name which interfaces deserve the strict treatment and leave the rest lenient. ## A one-page version of the policy 1. Wrap mocks in `NiceMock` unless the interface is on the strict list; switch a mock to naggy temporarily while writing a test to see every call. 2. Stub with `ON_CALL` in fixtures; each test adds the fewest `EXPECT_CALL`s that prove its named behaviour. 3. Forbid harmful calls explicitly with `Times(0)` in the tests that care. 4. Mock only interfaces the team owns; foreign APIs go behind adaptors. 5. One shared mock per interface, in a test-only library beside it.

  • A senior engineer argues StrictMock everywhere catches more bugs. How do you answer?
    It catches extra calls, but it also fails on harmless ones, so every refactor that adds a log, a cache check or a self-test edits unrelated tests. Those failures train people to update expectations mechanically, which hides real bugs. I would agree to strict mocks for interfaces where every call is contract, and ask for explicit `Times(0)` expectations elsewhere.
  • Where should a mock of an interface owned by another team live?
    Next to that interface, in one test-only library, as GoogleMock's guidance suggests. Then a change to the interface updates one mock, and only tests that use the changed methods need attention. Better still, depend on an adaptor interface your team owns, so their changes are absorbed in one adaptor.

  • Mandating StrictMock everywhere so every extra call fails
  • Letting each test file define its own copy of the same mock
  • Mocking a third-party library's classes directly
  • Requiring an EXPECT_CALL for every collaborator call to prove coverage
  • Flipping --gmock_default_mock_behavior on an existing suite without a migration