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.
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.
As of October 30th (the repository may evolve in GitHub in the future):
CLOSE DATASETDELETE DATASETMESSAGESY-MSGNO, etc.)OPEN DATASETTRANSFERWRITE/: (write on a new line), not / (write on the current line), colors, position in the current line.WRITE TOAs of October 30th (the repository may evolve in GitHub in the future):
SY-DATUMSY-LINSZSY-REPIDSY-SLSETSY-TITLESY-UNAMESY-UZEITSandra
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.