4

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..

Below Singleton Class to be Gtested:

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_;
};

I tired GTest using Friend class like below:

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();
}

Throws Error Like below:

 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);
273K
  • 29,503
  • 10
  • 41
  • 64
Ragav
  • 325
  • 1
  • 2
  • 13
  • Why is it private? – Antonio Pérez Mar 15 '16 at 15:06
  • @Antonio Pérez: i dont have the reason for making that as private. Since the Source code to be tested was not written by me :-). But the Usecase is how to Gtest a Singleton Class Private Method... – Ragav Mar 15 '16 at 15:09
  • @Mark B.. My Question is different from the other by the way it is related to Singleton and more Piece of exact code written is Posted with clarity .Also the other post doesn't fully propose the Proper way to handle this kind of Testing . – Ragav Mar 15 '16 at 15:11
  • 1
    @Ragav It shouldn't matter if the object instance you're testing is a singleton or an instance of anon-singleton. You'll test private methods the same way (preferably NOT by calling them directly but through the public interface). – Mark B Mar 15 '16 at 15:14
  • @Mark B: Thanks. Do you Still think that is the Proper way of Google Test by using the #define alone? It is Still Debatable. and singleton itslef can be implemented in various ways.. so this platform can be a complete variants of dealing with them. – Ragav Mar 15 '16 at 15:17
  • 1
    @Ragav Mark B expressed it very nicely. Your test is a _client_ of the `Listener` class, so you should be using only its public interface. If the private method is complex enough to deserve being tested in isolation, then you should redesign your code and probably factor out a new class. – Antonio Pérez Mar 15 '16 at 15:19
  • @ Antonio Pérez: I agree your view.. Thanks for input!!.. As i said earlier.. Source code is not mine and i have no rights to modify it. :-) – Ragav Mar 15 '16 at 15:23
  • @Ragav then why do you need to test it? What if you find a bug as an outcome of testing, are you not allowed to fix it? – Antonio Pérez Mar 15 '16 at 15:45

1 Answers1

2

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.

Marc O'Morain
  • 3,699
  • 5
  • 28
  • 34
  • Is it a common practice ? – Pierre Mar 15 '16 at 15:01
  • @Marc O'Morain : Thanks ... I will try adding this # define before the #include in the CPP Gtest File and Let you know. – Ragav Mar 15 '16 at 15:02
  • @PierreI I hope it isn't a common practice :) I used to have to do it when trying to work with game development middleware. Sometimes fields that you needed access to were marked `private`. – Marc O'Morain Mar 15 '16 at 15:05
  • @Marc O'Morain : Thank You Again !! it doesnt works for below case. Please see below: – Ragav Mar 18 '16 at 23:06
  • @Ragav the problem is that there is no `private:` declaration above the setAligmentFlags function. You should add an explicit `private:` keyword above it. – Marc O'Morain Mar 18 '16 at 23:24