Skip to content

How to load mockups

atsy edited this page Jul 6, 2016 · 2 revisions

Here is a small guide on how to start with the ABAP Mockup loader.

Assume you have prepared a TAB-delimited text file with a header line for you unit test, like this (BKPF table in example):

BUKRS  GJAHR  BELNR    BLART  BLDAT       XBLNR  BKTXT
ZZZZ   2016   2000000  KR     01.02.2016  10/1   Invoice for goods
ZZZZ   2016   2000001  KR     02.02.2016  10/2   Invoice for more goods
ZZZZ   2016   3000000  KZ     10.02.2016  88897  Payment for 10/1

And you want to load it into an internal table (or just the 1st line to a structure). This could be raw data for your unit test or the result you expect from some method or procedure.

Here are the steps to do it

  1. Ensure your file is saved in UTF16 encoding (you may use international characters consequently). Pack it with ZIP. Of course you can keep multiple files there in multiple directories.

  2. Decide where you want to keep this zip. It can be either a file at your machine or you can upload it directly to SAP with SMW0 transaction as binary data. Then it will be a part of your package and will "travel" in transports with the rest of your code. For the example I'll be keeping the file in 'c:\sap' directory on my laptop. So here is the code you have to add before any other call to mockup loader methods. In a unit test the best place for it is class_setup() method:

  zcl_mockup_loader=>class_set_source( i_type = 'FILE' i_path = 'c:\sap\mymockup.zip' ).

2.1) Optionally, you can also define amounts format - first char is thousand separator, second char is decimal separator. With the code below you can use figures like '1234,56' or '1 234,56'. Decimal positions depend on receiving data type - it is not always 2.

  zcl_mockup_loader=>class_set_params( i_amt_format = ' ,' ).
  1. Now you can actually get your instance of the mockup loader. In a unit test the best place for this is setup() method. Note that the mockup loader is a singleton class so zip file is actually read once per unit test class (for performance reasons).
  method setup. 
    data lo_ex      type ref to cx_static_check.

    try.
      o_ml = zcl_mockup_loader=>get_instance( ).
    catch cx_static_check into lo_ex.
      cl_aunit_assert=>fail( lo_ex->get_text( ) ).
    endtry.

    " ... some other code you want here

  endmethod.
  1. Assuming you put your file into TEST1 directory in zip file and called it bkpf.txt (names are case sensitive). Now you load the data to an internal table ...
  data lt_bkpf type table of bkpf.

  o_ml->load_data( exporting i_obj = 'TEST1/bkpf' i_strict = abap_false
                   importing e_container = lt_bkpf ).

... or a structure (just the first line will be extracted) ...

  data ls_bkpf type bkpf.

  o_ml->load_data( exporting i_obj = 'TEST1/bkpf' i_strict = abap_false
                   importing e_container = ls_bkpf ).

i_strict = abap_false presumes that not all of the fields in target structure are present in the text file. In the example BKPF table does have more fields than the example text at the beginning.

  1. That's all ! :)

Practical notes

In our projects we have many tests and much data. We keep the test data as a single binary object (zip) in SMW0. Each test class have a correspondent directory in zip file. Also we care about code visibility and so incapsulate unnecessary staff in macros like this:

  define load_mockup_no_strict.
    call method o_ml->load_data
      exporting
        i_obj       = 'EVENT/' && &1
        i_strict    = abap_false
      importing
        e_container = &2.
  end-of-definition.
  ...
  " Load                 'FILE   'CONTAINER
  load_mockup_no_strict  'bkpf'  me->at_bkpf.
  load_mockup_no_strict  'bseg'  me->at_bseg.
  load_mockup_no_strict  'bset'  me->at_bset.
  ...