2023 Sep 25 5:03 PM
Ho can I create a class which as a global class as a friend and a local class as friend the same time.
Background:
The class has a private constructor which only can be called from the global friend class.
I want to write a unit test now. For that I also must call the private constructor but from the local test class.
2023 Sep 25 5:29 PM
Hi Ralf,
in the public section of the global class you can declare the global friend (or in se24 at 1st tab):
CLASS zcl_global DEFINITION PUBLIC
CREATE PRIVATE
GLOBAL FRIENDS zcl_friend.
...
In the unit test include you can write:
CLASS lcl_test DEFINITION FOR TESTING ...
...
ENDCLASS.
CLASS zcl_global DEFINITION LOCAL FRIENDS lcl_test.
Now the test class can create objects of that class.
BR,
Holm
2023 Sep 25 5:29 PM
Hi Ralf,
in the public section of the global class you can declare the global friend (or in se24 at 1st tab):
CLASS zcl_global DEFINITION PUBLIC
CREATE PRIVATE
GLOBAL FRIENDS zcl_friend.
...
In the unit test include you can write:
CLASS lcl_test DEFINITION FOR TESTING ...
...
ENDCLASS.
CLASS zcl_global DEFINITION LOCAL FRIENDS lcl_test.
Now the test class can create objects of that class.
BR,
Holm
2023 Sep 25 7:38 PM
Syntax error. It should be:
CLASS lcl_test DEFINITION DEFERRED.
CLASS zcl_global DEFINITION
LOCAL FRIENDS
lcl_test.
CLASS lcl_test DEFINITION FOR TESTING ...
...
ENDCLASS.
2023 Sep 26 6:59 AM
sorry, cannot see that there is a syntax error if you place the "local friend" definition after the definition of lcl_test.
2023 Sep 26 7:11 AM
Okay. It would be a syntax error if there's a reference to ZCL_GLOBAL inside LCL_TEST definition.
It also depends whether CLASS lcl_test IMPLEMENTATION is above or below CLASS zcl_global DEFINITION.
I prefer my proposal because it's always syntax error free.
2023 Sep 26 8:50 AM
2023 Sep 26 4:22 PM
I had to put these declarations into the section: Class-relevant Local Types (Eclipse).
Not it works. Thank you very much.
2023 Sep 26 6:20 PM
ralfhs It's not required to have them in the Class-relevant Local Types if you use them only in the Test Classes.
2023 Sep 28 4:56 PM
My proposal is also the same as SAP proposal 😉
(see link in answer by Wolfgang)
2023 Sep 28 8:58 AM
2023 Sep 28 9:04 AM