ABAP Blog Posts
cancel
Showing results for 
Search instead for 
Did you mean: 
Sandra_Rossi
Active Contributor
516

When writing ABAP Unit, it may be needed to mock ABAP keywords like OPEN DATASET, TRANSFER, MESSAGE, WRITE or system variables like SY-UZEIT, SY-UNAME.

As far as I know, still in the latest ABAP version 7.58, there are no test doubles proposed for them.

I published the code in GitHub with MIT License: sandraros/abapDouble: Wrapper and test doubler of ABAP Language elements OPEN DATASET, WRITE...

The main class is ZCL_ABAP.

It handles few ABAP statements and doesn't handle the many possibilities of each proposed ABAP statement but you may easily adapt it.

This article is (of course) for private Cloud and on-premises ABAP systems.

Example how to use ZCL_ABAP

If you had this code:

DATA(file_path) = '/tmp/test'.
OPEN DATASET file_path FOR OUTPUT IN TEXT MODE ENCODING UTF-8.
DATA(text) = |Hello world, now is { sy-uzeit TIME = USER }|.
TRANSFER text TO file_path.
CLOSE DATASET file_path.

Convert it to this code:

DATA(abap) = zcl_abap=>get_singleton( ).
DATA(file_path) = '/tmp/test'.
abap->open_dataset( iv_file_path = file_path
                    iv_for       = abap->cs_open_dataset-for-output
                    iv_in_mode   = abap->cs_open_dataset-in_mode-text
                    iv_encoding  = abap->cs_open_dataset-encoding-utf_8 ).
abap->transfer( iv_data      = |Hello world, now is { abap->get_sy_uzeit( ) TIME = USER }|
                iv_file_path = file_path ).
abap->close_dataset( iv_file_path = file_path ).

Test it this way:

METHOD first_test. " declared FOR TESTING
  " GIVEN now is 152800 (fake)
  DATA(abap) = ztd_abap=>get_singleton( ).
  abap->set_sy_uzeit( '152800' ).

  " WHEN code under test is called
  call_cut( ).

  " THEN the file contains this data with fake 152800
  cl_abap_unit_assert=>assert_equals( act = abap->get_dataset( '/tmp/test' )-content
                                      exp = VALUE string_table( ( |Hello world, now is 15:28:00| ) ) ).
ENDMETHOD.

 

List of supported ABAP keywords

As of October 30th (the repository may evolve in GitHub in the future):

  • CLOSE DATASET
  • DELETE DATASET
  • MESSAGE
    • Limited to text, message type, display like. It also gives the possibility to re-send the current system message (variables SY-MSGNO, etc.)
  • OPEN DATASET
    • Limited to text mode, encodings default and UTF-8
  • TRANSFER
  • WRITE
    • Limited to character-like text and to these options /: (write on a new line), not / (write on the current line), colors, position in the current line.
  • WRITE TO
    • Limited to character-like text.

List of supported system variables

As of October 30th (the repository may evolve in GitHub in the future):

  • SY-DATUM
    • Use ZCL_ABAP=>GET_SINGLETON( )->GET_SY_DATUM( )
  • SY-LINSZ
  • SY-REPID
  • SY-SLSET
  • SY-TITLE
  • SY-UNAME
  • SY-UZEIT

Sandra

1 Comment
BjoernSchulz
SAP Mentor
SAP Mentor

Hey @Sandra_Rossi ,

good idea, not only in terms of testability, but also when you're looking towards ABAP Cloud. My recommendation would be to make the methods "clean" right from the start, so also new developers know what SY-UZEIT is 😁

I did the same for ABAP Cloud in terms of testability and reusability to get easy access to system information. Here because the new fields are spread over three objects.

Greetings

Björn

Top liked authors