CDS View Unit Test with Test Doubles

light111CDS View Unit Test with Test Doubles


Performing unit test of the development objects are becoming a compulsory part of the development process. With new CDS entities having their code push down feature the ABAP unit test framework provides features called CDS test double framework.

The below details just simple introduces how to use the CDS test double framework for a CDS view.  To perform a unit test its a decision taken what features to be tested  when the cds view performs a complex calculation, but as this is just a beginning to understand the cds test double framework, here the cds view performs a simple calculation.

Lets begin with the FLIGHT model.

The below SLIGHT table contains 355 entries.

1

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.

2

The CDS view output with just 26 rows.

6

Let’s create a global class(Tx-SE24) with local Test class.

3

 

4

Perform the UNIT Test.

5


The details of the local test class.

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-DATA: lo_td_sflight TYPE REF TO if_cds_test_data.

DATA: lo_sflight_double TYPE REF TO if_cds_stub.

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.

” call the CREATE method to create the environment
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.

” here we prepare the test double data as not intended to test with the actual DB data

* 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’ )
).
lo_td_sflight = cl_cds_test_data=>create( i_data = lt_sflight ).

the dependent component name
lo_sflight_double = lo_enviro->get_double( i_name = ‘SFLIGHT’ ).
lo_sflight_double->insert( i_test_data = lo_td_sflight ).

ENDMETHOD.

METHOD teardown.
lo_enviro->clear_doubles( ).
ENDMETHOD.
METHOD test_group_price.

” perform the assert testing – compare the actual with the expected values
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’ )
).

” the selection on the CDS view gives the test double data  
SELECT * FROM zdemo_sflight INTO TABLE @DATA(lt_act_group_res).

” 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.


Sometimes the CDS view is associated with the authorization checks via DCL. During the unit test, the DCL can be disabled by passing the correct flag to the highlighted parameter when creating the environment for the CDS view(s).

7


 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s