Application Development 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: 

Unit Testing Class: How to capture CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD and UPLOAD_XLS_FILE_2_ITAB

former_member209920
Active Participant
0 Kudos
521

Hi Gurus,

I am doing the unit testing classes for the first time. I had covered all other codes, but struck at methods where we are reading file (Text or Excel ) from application server using CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD and UPLOAD_XLS_FILE_2_ITAB function module / method. I cannot use static address from my system Or desktop to read test files as it will not work someone else system.

Can someone provide some insights who to cover these methods during unit testing ?

3 REPLIES 3

FredericGirod
Active Contributor
335

Hi manu.bhatnagar

You are suppose to test the responsibility of your public method, not external method.

The external class/tables/... should be bypass using Mock or other tips & tricks

matt
Active Contributor
335

For calls to external methods, Function Modules and database access, I use a separate class (I call it the DAO - Data Access Object). I defined methods which will call CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD and UPLOAD_XLS_FILE_2_ITAB function module, with the same parameters. This class is an implementation of an interface. In my testing code, I create a test double class that implements the interface and provides fixed data for testing.

E.g.

INTERFACE zif_myapp_dao.
METHODS:
gui_upload...
upload_xlsx...
END INTERFACE.
CLASS zcl_myapp_dao DEFINITION.
PUBLIC SECTION.
INTERFACES zif_myapp_dao.
ENDCLASS.
CLASS zcl_myapp_dao IMPLEMENTATION.
METHOD zif_myapp_dao~gui_upload.
CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD...
ENDMETHOD.
METHOD zcl_myapp_dao~upload_xlsx.
CALL FUNCTION 'UPLOAD_XLS_FILE_2_ITAB'...
ENDMETHOD.
ENDCLASS.

In my program, I use something like this for the productive code:

CLASS zmyapp DEFINITION. 
DATA dao TYPE REF TO zif_myapp_dao.
METHODS constructor
IMPORTING i_dao TYPE REF TO zif_myapp_dao OPTIONAL.
...
METHOD constructor.
IF i_dao IS SUPPLIED.
dao = i_dao.
ELSE.
dao = new zcl_myapp_dao( ).
ENDIF.
ENDMETHOD.
METHOD that_uses_gui_upload.
dao->gui_upload( ... ).
ENDMETHOD.
METHOD that_user_the_fm.
dao->upload_xlsx( ... ).
ENDMETHOD.

In my test code I use this:

CLASS ltd_dao DEFINITION FOR TESTING.
PUBLIC SECTION.
INTERFACES zif_myapp_dao.
ENDCLASS.
...
CLASS ltc_myapp DEFINITION FOR TESTING...
PRIVATE SECTION.
DATA dao TYPE REF TO ltd_dao.
DATA cut TYPE REF TO zmyapp.
...
CLASS ltc_myapp IMPLEMENTATION.
METHOD setup.
dao = new #( ).
cut = new #( CAST zif_myapp_dao( dao ) ).

In this way, any calls in your productive program which would consume the SAP methods/function modules are passed to ltd_dao instead of zcl_myapp_dao, and so you have full control over what is returned.

FredericGirod
Active Contributor
335

This is a Mock matthew.billingham

You could create it global, and inject the mock during the test