c++ – Can resolved gmock MOCK_METHOD marked ‘override’, but does not override

Hi I’m trying to get a googleTest mock to compile with bazel in windows and I can’t get arround the error ChargerSupportMock::GetIbatt()’ marked ‘override’, but does not override

I feel I’m so close to the GMock for Dummies example but I can’t find the fix. gMock for Dummies

I can run a basic test without mock no problem so the base bazel / MinGw configuration seem to work.

Here is the file I’m trying to build:

#include <gtest/gtest.h>
#include <gmock/gmock.h>  // Brings in gMock.


class IChargerSupport
{
public:
    virtual ~IChargerSupport() {}
    virtual void GetVbatt() = 0;
    virtual void GetIBatt() = 0;
};
using ::IChargerSupport;
class ChargerSupportMock : public IChargerSupport {
 public:

    MOCK_METHOD(void, GetVbatt, (), (override));
    MOCK_METHOD(void, GetIbatt, (), (override));

};

//using ::testing::NiceMock;
//using ::testing::AtLeast;

// Demonstrate some basic assertions.
TEST(Charger, BasicAssertions) {

    EXPECT_EQ(0, 0);

}

The command to start the test is
bazel.exe test –verbose_failures –compiler=mingw-gcc –test_output=all //UnitTest:AllTests

The build file is as follows

load("@rules_cc//cc:defs.bzl", "cc_library")

test_suite(
    name = "AllTests",
    tests = [
        "ChargerTest",
        "DummyTest",
    ],
)

cc_test(
  name = "ChargerTest",
  size = "small",
  srcs = ["Charger_test.cpp"],
  copts = ["-IBusinessDev/Inc"],
  deps = ["@com_google_googletest//:gtest_main",
         "//BusinessDev:Charger"],
)

cc_test(
  name = "DummyTest",
  size = "small",
  srcs = ["Dummy_test.cpp"],
  copts = ["-IBusinessDev/Inc"],
  deps = ["@com_google_googletest//:gtest_main",
         "//BusinessDev:Dummy"],
)

cc_library(
  name = "ChargerTest2",
  srcs = ["Charger_test.cpp"],
  copts = ["-IBusinessDev/Inc"],
  deps = ["@com_google_googletest//:gtest_main",
         "//BusinessDev:Charger"],
)

The output is:

Loading: 
Loading: 0 packages loaded
Analyzing: 2 targets (0 packages loaded, 0 targets configured)
INFO: Analyzed 2 targets (0 packages loaded, 0 targets configured).
INFO: Found 2 test targets...
[0 / 6] [Scann] Compiling googletest/src/gtest-test-part.cc ... (2 actions, 0 running)
[1 / 7] Compiling googletest/src/gtest-test-part.cc; 1s local ... (3 actions running)
ERROR: C:/dev/abb/self-contained/selfcontained/UnitTest/BUILD:11:8: Compiling UnitTest/Charger_test.cpp failed: (Exit 1): gcc failed: error executing command (from target //UnitTest:ChargerTest) 
  cd /d C:/users/nmercure/_bazel_nathaniel.mercure/7fsxykth/execroot/__main__
  SET PATH=c:/msys64/mingw64/bin
    SET PWD=/proc/self/cwd
    SET RUNFILES_MANIFEST_ONLY=1
  c:msys64mingw64bingcc -std=gnu++0x -MD -MF bazel-out/x64_windows-fastbuild/bin/UnitTest/_objs/ChargerTest/Charger_test.d -frandom-seed=bazel-out/x64_windows-fastbuild/bin/UnitTest/_objs/ChargerTest/Charger_test.o -iquote . -iquote bazel-out/x64_windows-fastbuild/bin -iquote external/com_google_googletest -iquote bazel-out/x64_windows-fastbuild/bin/external/com_google_googletest -iquote external/bazel_tools -iquote bazel-out/x64_windows-fastbuild/bin/external/bazel_tools -isystem external/com_google_googletest/googlemock -isystem bazel-out/x64_windows-fastbuild/bin/external/com_google_googletest/googlemock -isystem external/com_google_googletest/googlemock/include -isystem bazel-out/x64_windows-fastbuild/bin/external/com_google_googletest/googlemock/include -isystem external/com_google_googletest/googletest -isystem bazel-out/x64_windows-fastbuild/bin/external/com_google_googletest/googletest -isystem external/com_google_googletest/googletest/include -isystem bazel-out/x64_windows-fastbuild/bin/external/com_google_googletest/googletest/include -IBusinessDev/Inc -c UnitTest/Charger_test.cpp -o bazel-out/x64_windows-fastbuild/bin/UnitTest/_objs/ChargerTest/Charger_test.o
# Configuration: 0335715fb2d1accc7d914b611ab3585438de820e458a0d90c2e05fb3f44baace
# Execution platform: @local_config_platform//:host
In file included from external/com_google_googletest/googlemock/include/gmock/gmock-actions.h:147,
                 from external/com_google_googletest/googlemock/include/gmock/gmock.h:59,
                 from UnitTest/Charger_test.cpp:2:
UnitTest/Charger_test.cpp:19:27: error: 'testing::internal::Function<void()>::Result ChargerSupportMock::GetIbatt()' marked 'override', but does not override
   19 |         MOCK_METHOD(void, GetIbatt, (), (override));
      |                           ^~~~~~~~
UnitTest/Charger_test.cpp: In member function 'virtual void Charger_BasicAssertions_Test::TestBody()':
UnitTest/Charger_test.cpp:28:38: error: cannot declare variable 'chargerMock' to be of abstract type 'testing::NiceMock<ChargerSupportMock>'
   28 |         NiceMock<ChargerSupportMock> chargerMock;
      |                                      ^~~~~~~~~~~
In file included from external/com_google_googletest/googlemock/include/gmock/gmock.h:65,
                 from UnitTest/Charger_test.cpp:2:
external/com_google_googletest/googlemock/include/gmock/gmock-nice-strict.h:136:39: note:   because the following virtual functions are pure within 'testing::NiceMock<ChargerSupportMock>':
  136 | class GTEST_INTERNAL_EMPTY_BASE_CLASS NiceMock
      |                                       ^~~~~~~~
UnitTest/Charger_test.cpp:12:22: note:     'virtual void IChargerSupport::GetIBatt()'
   12 |         virtual void GetIBatt() = 0;
      |                      ^~~~~~~~
[4 / 7] 1 / 2 tests, 1 failed; checking cached actions
INFO: Elapsed time: 5.288s, Critical Path: 4.66s
INFO: 4 processes: 4 internal.
FAILED: Build did NOT complete successfully
//UnitTest:DummyTest                                                  NO STATUS
//UnitTest:ChargerTest                                          FAILED TO BUILD

Executed 0 out of 2 tests: 1 fails to build and 1 was skipped.
FAILED: Build did NOT complete successfully

Leave a Comment