Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP Class global fiendship and local friendship

ralfhs
Explorer
0 Kudos
1,895

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.

1 ACCEPTED SOLUTION

holm
Participant
1,722

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

10 REPLIES 10

holm
Participant
1,723

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

Sandra_Rossi
Active Contributor
0 Kudos
1,722

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.

0 Kudos
1,722

sorry, cannot see that there is a syntax error if you place the "local friend" definition after the definition of lcl_test.

Sandra_Rossi
Active Contributor
1,722

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.

0 Kudos
1,722

Right 🙂 But i hate these forward declarations...

0 Kudos
1,722

I had to put these declarations into the section: Class-relevant Local Types (Eclipse).

Not it works. Thank you very much.

Sandra_Rossi
Active Contributor
0 Kudos
1,722

ralfhs It's not required to have them in the Class-relevant Local Types if you use them only in the Test Classes.

Sandra_Rossi
Active Contributor
0 Kudos
1,722

My proposal is also the same as SAP proposal 😉

(see link in answer by Wolfgang)

wolfgang_woehrle
Product and Topic Expert
Product and Topic Expert
1,722

Hi,

you can also see here our help content for SAP BTP.

KR

Wolfgang

0 Kudos
1,722

Thanks for that. Finally it's working also now like that.