I need to Gtest a Private Method of the singleton class. I tried by using Friend class of the singleton. but no help. It says the Private cant be called. i added the class, Gtest tried, Output error. feel free to ask any more clarifcation, thanks in advance!! Please ignore the Typos if any..
class Listener: public CommonListen {
public:
friend class test_listener;
Listener(task_id taskid, const string thread_name) :
CommonListen(taskid, thread_name ) { }
static Listener* GetInstance() {
if (Listener_ptr_ == nullptr) {
Listener_ptr_ = new
Listener(LISTENER_ID, ListenerName);
}
return Listener_ptr_;
}
private:
// Override the base's method
void SetupPub();
static Listener* Listener_ptr_;
};
class test_listener : public ::testing::Test {
public:
void call_private(void);
};
TEST_F(test_listener, create_listener) {
call_private();
}
void test_listener::call_private() {
(Listener::GetInstance())->SetupPub();
}
error: 'virtual void Listener::SetupPub()' is private
void SetupPub();
^
test_listener.cc:48:63: error: within this context
(Listener::GetInstance())->SetupPub();
^
make[2]: *** [test_listener.o] Error 1
Please share your view
@@@@Not Working Case:@@@@
class xxx : public yyy {
friend class test_xxx;
enum class State : std::int8_t { SSS = 1,
DDD, FFF};
enum class AlignmentFlags : std::int8_t { ZZZ= 1,
CCC= 2, VVV= 4, BBB= 8,
NNN= 3, MMM= 12};
public:
// **********
}
While calling the above private enum class after "#define private public" used below erroroccurs:
../src/qqq/xxx.h:189:14: error: 'enum class
xxx::AlignmentFlags' is private
enum class AlignmentFlags : std::int8_t { ZZZ = 1,
Accessed Like :
xxx->SetAlignmentFlags(12);
You can add the following as the very first line of your GTest cpp file, before you include anything else:
#define private public
This will make your tests have access to all private members of your class, since they will be compiled as public.