How can I test private or protected operations?
Sample model: Samples/CppSamples/TestConductor/TestingCookbook/CppFractionsPrivateOperations
Sample model: Samples/CSamples/TestConductor/TestingCookbook/CFractionsPrivateOperations
TestConductor supports unit testing of one or multiple classes (or similar elements which are generated into one translation unit) in isolation, by stimulating the system under test (SUT) using the interface methods (events, operations, triggered operations) which can be accessed from outside of the SUT. This means per default only public operations can be invoked directly during test execution while private (C) or private and protected (C++) operations can be called only indirectly. This might prevent for example testing of private or protected operations with certain argument values which would be necessary to test all scenarios or to achieve full code coverage.
To support testing of private or protected operations, TestConductor optionally generates public wrapper operations. This option can be enabled in the code generation configuration used for testing by checking the check box PrepareForTestingOfPrivateOps. If this option is enabled, each time the user invokes Build TestPackage (or Build TestContext or Build TestCase), after the code has been generated by Rhapsody, TestConductor generates wrapper operations for all private and protected operation of the SUT which exist in the model. The wrapper operations have the same code signature as the original operation in the generated code but they are declared public and the names have the prefix callprivate_. The wrapper operations are generated into a header file with the same name as the original source code file prefixed with publicwrap_ and an #include statement for this header file is added to the implementation file of the class.
The wrapper operations can be used for example in code based test cases or in test actions of SD based test cases. Private or Protected operations cannot be selected as realization of messages in the Rhapsody SD editor so they cannot be specified this way. For reference, see the code test cases in the sample model. Using wrapper operations, full code coverage of the private and protected operations of the tested class is achieved.
Note: If the option to test private or protected operations is enabled, TestConductor modifies the implementation file of the SUT after it has been generated (#include statement is added). Because of this, Rhapsody offers to round trip the modified file into the model the next time code is generated which is not recommended. When switching off this option the user should force a regeneration of code by using Rhapsody menu Code, Regenerate once.