0

I have some experience in C and C++. But I am new to googletest and googlemock. I am trying unit testing for C++ Programs. I did not get any error while I was working on googletest. But, when I started working on googlemock, I got problems. I have a simple code for working with googlemock, and when I tried to build it with the command

g++ mock.cpp -lgtest -lgtest_main -lgmock -pthread

I get this error which I am not able to understand. (I have attached both the program and the error which I got).

Please help me to understand it and overcome. Thanks in advance.

mock.cpp

#include<iostream>
#include<vector>
#include<gtest/gtest.h>
#include<gmock/gmock.h>

using namespace std;
using ::testing::AtLeast;
using ::testing::Return;
using ::testing::_;


class DataBaseConnect{
    public:
        virtual bool login(string username, string password){ return true; }
        virtual bool logout(string username){ return true; }
        virtual int fetchRecord(){ return -1; }
};


class MockDB : public DataBaseConnect{
    public:
        MOCK_METHOD0(fetchRecord, int());
        MOCK_METHOD1(logout, bool(string username));
        MOCK_METHOD2(login, bool(string username, string password));
};


class MyDatabase{
    DataBaseConnect & dbC;
    public:
        MyDatabase(DataBaseConnect & _dbC) : dbC(_dbC) {}

        int Init(string username, string password){
            if(dbC.login(username, password) != true){
                cout<<"DB FAILURE\n";
                return -1;
            }
            else{
                cout<<"DB SUCCESS\n";
                return 1;
            }
        }
};


TEST(MyDBTest, LoginTest){
    //Arrange
    MockDB mdb;
    MyDatabase db(mdb);

    EXPECT_CALL(mdb, login(_, _))
    .Times(1)
    .WillOnce(Return(true));

    //Act
    int retVal = db.Init("Terminator", "I'm Back");

    //Assert
    EXPECT_EQ(retVal, 1);
}


int main(int argc, char **argv){
    testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}

command:

g++ mock.cpp -lgtest -lgmock -lgtest_main -pthread

error:

g++ mock.cpp -lgtest -lgmock -lgtest_main -pthread
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::linked_ptr_internal::join(testing::internal::linked_ptr_internal const*)':
mock.cpp:(.text._ZN7testing8internal19linked_ptr_internal4joinEPKS1_[_ZN7testing8internal19linked_ptr_internal4joinEPKS1_]+0x2a): undefined reference to `testing::internal::g_linked_ptr_mutex'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::linked_ptr_internal::depart()':
mock.cpp:(.text._ZN7testing8internal19linked_ptr_internal6departEv[_ZN7testing8internal19linked_ptr_internal6departEv]+0x27): undefined reference to `testing::internal::g_linked_ptr_mutex'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<int ()>::InvokeWith(std::tuple<> const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFivEE10InvokeWithERKSt5tupleIJEE[_ZN7testing8internal18FunctionMockerBaseIFivEE10InvokeWithERKSt5tupleIJEE]+0x33): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<bool (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)>::InvokeWith(std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE10InvokeWithERKSt5tupleIJS7_EE[_ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE10InvokeWithERKSt5tupleIJS7_EE]+0x33): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<bool (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)>::InvokeWith(std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE10InvokeWithERKSt5tupleIJS7_S7_EE[_ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE10InvokeWithERKSt5tupleIJS7_S7_EE]+0x33): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<bool (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)>::AddNewExpectation(char const*, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::tuple<testing::Matcher<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, testing::Matcher<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE17AddNewExpectationEPKciRKS7_RKSt5tupleIJNS_7MatcherIS7_EESG_EE[_ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE17AddNewExpectationEPKciRKS7_RKSt5tupleIJNS_7MatcherIS7_EESG_EE]+0xda): undefined reference to `testing::Expectation::Expectation(testing::internal::linked_ptr<testing::internal::ExpectationBase> const&)'
collect2: error: ld returned 1 exit status
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • Possibly because you link `gtest` **before** the libraries that use parts of it? https://stackoverflow.com/a/409402/1171191 – BoBTFish Jun 15 '21 at 11:41
  • You didn't linked `gmock_main`. – Marek R Jun 15 '21 at 11:45
  • Try on this `g++ mock.cpp -lgmock -lgtest -lgtest_main -pthread` – prehistoricpenguin Jun 16 '21 at 03:51
  • Hello @prehistoricpenguin, I tried the command which you mentioned. But then, I got this huge number of errors which had 'cannot convert', 'invalid initialization' and more. There were more than 100 errors. – PREM KUMAR T S Jun 16 '21 at 10:35
  • Thank you @BoBTFish for your response. Now, I came to know why it we should take care of the sequence of the link libraries. – PREM KUMAR T S Jun 16 '21 at 10:38
  • Your code and compile command `g++ mock.cpp -lgtest -lgmock -lgtest_main -pthread` works normaly on my environemt. So your environment must have problems. How did you install gtest and gmock? If you manually build them, what instruction did you use? What's the version of them? What's your environment? – prehistoricpenguin Jun 16 '21 at 10:38
  • Thank you @MarekR for your response. I tried with gmock_main, and I still get the same error. – PREM KUMAR T S Jun 16 '21 at 10:39
  • @prehistoricpenguin I followed the instructions which were given on the below link. https://stackoverflow.com/a/13513907/16232713 Please let me know if there is any issue with the approach and how to resolve it. – PREM KUMAR T S Jun 16 '21 at 10:42
  • What's your Linux distro version and name? Mostly we can install gtest and gmock with a system package manager. – prehistoricpenguin Jun 16 '21 at 10:46
  • @prehistoricpenguin This is my distro. Distributor ID: Ubuntu Description: Ubuntu 20.04.2 LTS Release: 20.04 Codename: focal – PREM KUMAR T S Jun 16 '21 at 10:51
  • Or you can build from source code with a new version : https://github.com/google/googletest/archive/refs/tags/release-1.11.0.tar.gz , the code you used is out of dated, it have known issues: https://github.com/google/googletest/issues/2640 – prehistoricpenguin Jun 16 '21 at 10:51
  • Thank you for sharing this @prehistoricpenguin I will try building with this new version. – PREM KUMAR T S Jun 16 '21 at 10:54
  • Seems the tutorial you followd have forgot to install gmock.so, so after building, you need to type `sudo make install` to install all the libraries files. – prehistoricpenguin Jun 16 '21 at 10:57
  • After install gmock libraries, your problem should be fixed. – prehistoricpenguin Jun 16 '21 at 10:57

1 Answers1

0

Seems the tutorial you followed have forgotten to install gmock.so, so after building finished, you need to type sudo make install to install all the libraries file.

And the version of gtest you used is out of date, suggest to use this version:

https://github.com/google/googletest/archive/refs/tags/release-1.11.0.tar.gz

Other answers around your tutorial have also pointed out that, we need to install gmock libraries. Then the problem should be fixed.

prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42
  • Thank you. Now, the code compiles and works without any errors. From now on, I will keep in mind of the issues that are faced. – PREM KUMAR T S Jun 16 '21 at 11:57