Reputation: 21
is there a clean way to write a unit test of a productive code (especially the private methods) of global class CL_A
from local test class LCL_B
which is under the global test class TC_B
? So far I have figured out a trick, which is far from clean coding practice. TC_B
is a friend of CL_A
and LCL_B
is a local friend with TC_B
. If I want to test method M_A
of class CL_A
I have to create extra wrapper method M_B
in a global test class TC_B
which calls M_A
. See a pseudo code sample lower as this could be confusing without structured example.
Thanx!
CLASS CL_A definition global friends TC_B.
private:
METHODS: M_A.
CLASS TC_B definition for testing.
private:
METHODS: M_B
IMPORTING cut TYPE REF TO CL_A.
METHOD M_B.
cut->M_A().
ENDMETHOD.
CLASS LCL_B definition deferred.
CLASS TC_B definition local friends LCL_B.
CLASS LCL_B definition final for testing.
private:
DATA: cut TYPE REF TO CL_A.
env TYPE REF TO TC_B.
METHODS: test_M_A for testing,
setup.
METHOD setup.
CREATE OBJECTS cut, env.
ENDMETHOD.
METHOD test_M_A.
env->M_B( cut ).
ENDMETHOD.
Upvotes: 0
Views: 2526
Reputation: 5071
Test publics, not internals, because of the detailed reasoning given there.
If not possible, for example because you are caught up in legacy code, establish a global test class that is friends with the original class, and that exposes private methods as public methods.
CLASS original_class DEFINITION.
PRIVATE SECTION.
METHODS some_private_method ...
ENDCLASS.
CLASS test_wrapper DEFINITION PUBLIC CREATE PUBLIC.
PUBLIC SECTION.
METHODS public_wrapper_for_private ...
ENDCLASS.
CLASS test_wrapper DEFINITION GLOBAL FRIENDS original_class.
CLASS test_wrapper IMPLEMENTATION.
METHOD public_wrapper_for_private.
result = some_private_method( ).
ENDMETHOD.
ENDCLASS.
You can then create a local unit test class anywhere that tests test_wrapper
.
This solution is fragile and inferior however, and should at most be a temporary means until you get the chance to refactor the original class design.
Upvotes: 3