Skip to Content
1
Jul 31, 2023 at 03:31 PM

Unit testing database values after insertion

170 Views Last edit Aug 03, 2023 at 01:55 PM 5 rev

Hi experts,

I'm trying to use unit tests for writing access database tests, but I can't find a way to validate the correctness of inserted data. I'm using the ABAP SQL Test Double Framework : if_osql_test_environment.

I'm testing an insert method into the EKKO table, which is replace (with the framework) by the internal table MT_EKKO. While reading this table after the insert in the test environment, no new line is added. The test failed at my READ INDEX 2 on MT_EKKO. The insert return a sy-subrc of 0.

My goal is to check my new values in the MT_EKKO table to test in the future BAPI custom enhancement.

Is there a way to look up for data changes in the double table made with the framework?

REPORT.

CLASS lcl_test_bapi DEFINITION FINAL FOR TESTING DURATION SHORT RISK LEVEL HARMLESS .
PRIVATE SECTION.
CLASS-DATA:
mo_sql_env TYPE REF TO if_osql_test_environment.
DATA:
mt_ekko TYPE STANDARD TABLE OF ekko.
CLASS-METHODS:
class_setup,
class_teardown.
METHODS:
setup,
teardown,
test_insert FOR TESTING.
ENDCLASS.

CLASS lcl_test_bapi IMPLEMENTATION.
"Setup and teardown
METHOD class_setup.
mo_sql_env = cl_osql_test_environment=>create( i_dependency_list = VALUE #( ( 'EKKO' ) ) ).
ENDMETHOD.
METHOD class_teardown.
mo_sql_env->destroy( ).
ENDMETHOD.
METHOD setup.
mt_ekko = VALUE #( ( mandt = sy-mandt ebeln = '1111111111' bstyp = 'F' bsart = 'NB' aedat = '20230703' ernam = 'setup_method' ) ).
mo_sql_env->insert_test_data( mt_ekko ).
ENDMETHOD.
METHOD teardown.
CLEAR: mt_ekko.
mo_sql_env->clear_doubles( ).
ENDMETHOD.

"Test method
METHOD test_insert.

DATA ls_ekko TYPE ekko.
ls_ekko = VALUE #( mandt = sy-mandt ebeln = '1234567890' bstyp = 'F' bsart = 'NB' aedat = '20230101' ernam = 'FromInsert' ).
INSERT INTO ekko VALUES ls_ekko.
cl_abap_unit_assert=>assert_subrc( ).

READ TABLE mt_ekko INTO DATA(ls_test) INDEX 2.
cl_abap_unit_assert=>assert_subrc( msg = 'MT_EKKO doesn''t have a second line.' ). "Test failed here

cl_abap_unit_assert=>assert_equals(
EXPORTING
act = ls_test-ernam
exp = 'FromInsert' ).

ENDMETHOD.
ENDCLASS.

A good example of what I'm trying to do (with the MODIFY keyword) is the class LTC_TEST_DML_STMNTS from the package SABP_UNIT_DOUBLE_OSQL_DEMO. The downside of this approach is the code only check sy-subrc, and return the value to test. I will no be able to do that in my tests.

BR,

Antoine