0

I'm using the BoostTest Framework (version 1.57.0) with Turtle as mock libary (version 1.3.0). I'm able to access public and private functions in a test case, but I can't access protected functions. I'm able to mock these protected functions with MOCK_BASE_CLASS from Turtle. Example:

RealClass.hpp 
class RealClass
{
public: 
    void publicFunction();
protected:
    void protectedFunction();
private:
    void privateFunction();         
};

RealClass_UnitTest.hpp
BOOST_AUTO_TEST_SUITE( RealClass_UnitTest );

BOOST_AUTO_TEST_CASE( RealClass_publicFunction)
{
    RealClass  RealObject;
    RealObject.publicFunction()  // Can be accessed and tested
}

BOOST_AUTO_TEST_CASE( RealClass_protectedFunction)
{
    RealClass  RealObject;
    RealObject.protectedFunction()  // Error: realClass::protectedFunction() is protected
}

BOOST_AUTO_TEST_CASE( RealClass_privateFunction)
{
    RealClass  RealObject;
    RealObject.privateFunction()  // Can be accessed and tested
}

BOOST_AUTO_TEST_SUITE_END();

I don't understand why I can access RealObject.privateFunction(), but not RealObject.protectedFunction().

I didn't setup the testing framework, but I can't find any place where BoostTest is allowed to access private members.

Dave
  • 97
  • 2
  • 6
  • I wouldn't expect the test framework to be able to access private members. – Richard Hodges Oct 13 '17 at 11:13
  • Are you sure there isn't a `#define private public` somewhere? That would allow access to the private members. (It's strictly speaking undefined behaviour, but for a testing framework would probably be OK.) If so, you need a similar `#define protected public` in the same place. – Martin Bonner supports Monica Oct 13 '17 at 11:17
  • I thought I had searched the whole project. Unfortunately I didn't searched the submodules. There I found a `#define private public`. Is there a better way to do this? – Dave Oct 13 '17 at 11:33
  • 1
    In general, protected and private members shouldn't need to be tested. You should be testing them by invoking public members. The general advice is that if you feel the need to test private mechanisms, then your class is probably doing too much and should be split into separate classes to allow you to test the details directly. Basically, it's a design smell if you feel compelled to test private or protected members and the way to fix the smell is to change your design. – legalize Oct 13 '17 at 20:07

0 Answers0