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: 
Read only

Testing private methods: Declaring a local test friend

Martin_Bschen
Participant
11,987

I try to test the private methods of a class and therefor I declare a local test friend following this scheme:

class lcl_test definition deferred.
class zcl_testee definition local friends lcl_test.
...
class lcl_test definition for testing ...
...

This is been discussed in this wiki article or in this blog entry.

But all I get is the syntax error "The statement CLASS is unexpected".

What is wrong here?

My complete code:

CLASS lcl_test DEFINITION DEFERRED.
CLASS zcl_testee DEFINITION
 PUBLIC FINAL CREATE PUBLIC .
  PUBLIC SECTION.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS zcl_testee IMPLEMENTATION.
ENDCLASS.

CLASS lcl_test DEFINITION.
ENDCLASS.

CLASS lcl_test IMPLEMENTATION.
ENDCLASS.
1 ACCEPTED SOLUTION
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
5,910

Take my classes of package SABAP_DEMOS_CAR_RENTAL_APPL as an example, e.g. CL_DEMO_CR_RESERVATION_CATA. Check the includes where the test classes reside and where they are declared DEFERRED and as friends (local definitions and implementations).

I try to test the private methods of a class and therefor I declare a local test friend following this scheme:

class lcl_test definition deferred.
class zcl_testee definition local friends lcl_test.
...
class lcl_test definition for testing ...
...

This is been discussed in this wiki article or in this blog entry.

But all I get is the syntax error "The statement CLASS is unexpected".

What is wrong here?

My complete code:

CLASS lcl_test DEFINITION DEFERRED.
CLASS zcl_testee DEFINITION
 PUBLIC FINAL CREATE PUBLIC .
  PUBLIC SECTION.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS zcl_testee IMPLEMENTATION.
ENDCLASS.

CLASS lcl_test DEFINITION.
ENDCLASS.

CLASS lcl_test IMPLEMENTATION.
ENDCLASS.
7 REPLIES 7
Read only

pokrakam
Active Contributor
5,910

Where are you writing the code, report or class?

If report, then your class cannot be public. If it's in a class then the unit test class must be in the local test classes section, tab at the bottom of your editor if you use Eclipse, somewhere in the menu if you use SE24/80.

The all the code in your first block goes in the unit test section. The design here is that it doesn't even get compiled in a productive system, hence the strong separation.

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
5,911

Take my classes of package SABAP_DEMOS_CAR_RENTAL_APPL as an example, e.g. CL_DEMO_CR_RESERVATION_CATA. Check the includes where the test classes reside and where they are declared DEFERRED and as friends (local definitions and implementations).

Read only

5,910

That solved my question.

What was missing was the following code in the "Local Types"-Include

CLASS lcl_test DEFINITION DEFERRED.
CLASS zcl_testee DEFINITION LOCAL FRIENDS lcl_test

The class zcl_testee has a second definition in the "Global Class"-Include, without the LOCAL FRIENDS keyword.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
5,910

Is the behavior related to the ABAP version?

I have been defining the test class(es) local friends in the "Test Classes" include(CCAU). Never got an error in ABAP 740 & now on 750 too!

Read only

5,910

Placing the two lines in CCAU works for me in 7.40, 7.50 and higher.

But it is better style to place local declarations in CCDEF/CCIMP.

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
5,910

Thanks for the clarification! Going forward i'll stick to your recommendation.

Read only

0 Likes
5,910

Thank you ! , This is really helpful , Good Referance for Unit Testing