CDS View Unit Test
Lets begin with the FLIGHT model.
The below SLIGHT table contains 355 entries.
The CDS view on table SFLIGHT just performs a group by operation to give us the AMOUNT/PRICE based on CARRID, CONNID, CURRECNY and PLANETYPE.
The CDS view output with just 26 rows.
Let’s create a global class(Tx-SE24) with local Test class.
Perform the UNIT Test.
The details of the local test class.
“ use this source file for your ABAP unit test classes
CLASS lcl_demo_sflight_test DEFINITION FINAL FOR TESTING DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
CLASS-DATA: lo_enviro TYPE REF TO if_cds_test_environment.
CLASS-METHODS: class_setup.
CLASS-METHODS: class_teardown.
METHODS: setup.
METHODS: teardown.
* TEST METHODS
METHODS: test_group_price FOR TESTING.
ENDCLASS.
CLASS lcl_demo_sflight_test IMPLEMENTATION.
METHOD class_setup.
cl_cds_test_environment=>create(
EXPORTING
i_for_entity = ‘ZDEMO_SFLIGHT’ ” CDS VIEW NAME
RECEIVING
r_result = lo_enviro ).
ENDMETHOD.
METHOD class_teardown.
lo_enviro->destroy( ).
ENDMETHOD.
METHOD setup.
* insert test data into component doubles- sflight
DATA: lt_sflight TYPE TABLE OF sflight.
lt_sflight = VALUE #(
( mandt = sy-mandt carrid = ‘AA’ connid = ‘0001’ fldate = ‘01012020’ price = ‘100’ currency = ‘INR’ planetype = ‘DOM’ )
( mandt = sy-mandt carrid = ‘AA’ connid = ‘0001’ fldate = ‘01012020’ price = ‘200’ currency = ‘INR’ planetype = ‘DOM’ )
( mandt = sy-mandt carrid = ‘AA’ connid = ‘0001’ fldate = ‘02012020’ price = ‘500’ currency = ‘USD’ planetype = ‘INT’ )
( mandt = sy-mandt carrid = ‘AA’ connid = ‘0001’ fldate = ‘02012020’ price = ‘600’ currency = ‘USD’ planetype = ‘INT’ )
).
* insert the test data[ it creates test data to be tested and when we do select query this data is retrived]
lo_enviro->insert_test_data( EXPORTING i_data = lt_sflight ).
ENDMETHOD.
METHOD teardown.
lo_enviro->clear_doubles( ).
ENDMETHOD.
METHOD test_group_price.
DATA: lt_exp_group_res TYPE TABLE OF zdemo_sflight.
lt_exp_group_res = VALUE #(
( carrid = ‘AA’ connid = ‘0001’ amount = ‘1100.00’ currency = ‘USD’ planetype = ‘INT’ )
( carrid = ‘AA’ connid = ‘0001’ amount = ‘300.00’ currency = ‘INR’ planetype = ‘DOM’ )
).
” Selection from CDS View
SELECT * FROM zdemo_sflight INTO TABLE @DATA(lt_act_group_res).
” Perform Assert checks
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = lines( lt_act_group_res )
exp = lines( lt_exp_group_res )
msg = |No. of lines must be 2| ).
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = lt_act_group_res[ 1 ]-amount
exp = lt_exp_group_res[ 1 ]-amount
msg = |Amount must be 300| ).
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = lt_act_group_res[ 1 ]-currency
exp = lt_exp_group_res[ 1 ]-currency
msg = |Curency must be INR| ).
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = lt_act_group_res[ 1 ]-planetype
exp = lt_exp_group_res[ 1 ]-planetype
msg = |Plane type msut be DOM| ).
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = lt_act_group_res[ 2 ]-amount
exp = lt_exp_group_res[ 2 ]-amount
msg = |Amount must be 1100| ).
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = lt_act_group_res[ 2 ]-currency
exp = lt_exp_group_res[ 2 ]-currency
msg = |Curency must be USD| ).
cl_abap_unit_assert=>assert_equals(
EXPORTING
act = lt_act_group_res[ 2 ]-planetype
exp = lt_exp_group_res[ 2 ]-planetype
msg = |Plane type msut be INT| ).
ENDMETHOD.
ENDCLASS.