CDS View Unit Test with View Hierarchy

light111CDS View Unit Test with View Hierarchy


Below we have 3 CDS views with one level of hierarchy.

View- ZDEMO_SFLIGHT_LVL1 forms the base level with select on DB table.

View- ZDEMO_SFLIGHT_LVL2 forms the next level and finally

View- ZDEMO_SFLIGHT_LVL3 forms the next/final level  on the hierarchy.

1

Data Preview of View- ZDEMO_SFLIGHT_LVL3

2


Here is the class with Local Test class to perform the unit testing.

3

The CDS view- DEMO_SFLIGHT_LVL3 is used below. Now directly we can not fill data to table SFLIGHT as this is used in the very first level of the view- DEMO_SFLIGHT_LVL1.

So I_SELECT_BASE_DEPENDENCIES must be set as true, so that it enables the hierarchy testing for the CDS view DEMO_SFLIGHT_LVL3. Test Doubles created for all the entities in the last level of the hierarchy.

4

5

6


7

 


use this source file for your ABAP unit test classes
CLASS lcl_demo_flight_dep_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_flight FOR TESTING.

ENDCLASS.
CLASS lcl_demo_flight_dep_test IMPLEMENTATION.
METHOD class_setup.
cl_cds_test_environment=>create(
EXPORTING
i_for_entity = ‘ZDEMO_SFLIGHT_LVL3’        ” CDS VIEW NAME
i_select_base_dependencies = abap_true
RECEIVING
r_result = lo_enviro ).
ENDMETHOD.

METHOD class_teardown.
lo_enviro->destroy( ).
ENDMETHOD.

METHOD setup.

* insert test data into component
DATA: lt_sflight TYPE TABLE OF sflight.
lt_sflight = VALUE #(
( mandt = sy-mandt carrid = ‘A1’ connid = ‘0001’ fldate = ‘01012020’ price = ‘100’ currency = ‘USD’ planetype = ‘DOM’ )
( mandt = sy-mandt carrid = ‘A1’ connid = ‘0001’ fldate = ‘02012020’ price = ‘110’ currency = ‘INR’ planetype = ‘DOM’ )
( mandt = sy-mandt carrid = ‘A1’ connid = ‘0001’ fldate = ‘03012020’ price = ‘120’ currency = ‘USD’ planetype = ‘DOM’ )
( mandt = sy-mandt carrid = ‘A1’ connid = ‘0001’ fldate = ‘04012020’ price = ‘120’ currency = ‘USD’ planetype = ‘INT’ )
( mandt = sy-mandt carrid = ‘A1’ connid = ‘0001’ fldate = ‘05012020’ price = ‘120’ currency = ‘INR’ planetype = ‘INT’ )
).
* insert the test data
lo_enviro->insert_test_data( EXPORTING i_data = lt_sflight ).
ENDMETHOD.

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

DATA: lt_exp_res TYPE TABLE OF zdemo_sflight_lvl3.
lt_exp_res = VALUE #(
( carrid = ‘A1’ connid = ‘0001’ fldate = ‘01012020’ price = ‘100’ currency = ‘USD’ planetype = ‘DOM’ )
( carrid = ‘A1’ connid = ‘0001’ fldate = ‘03012020’ price = ‘120’ currency = ‘USD’ planetype = ‘DOM’ )
).

” Select in CDS view(with parameter)
SELECT * FROM zdemo_sflight_lvl3( p_currency = ‘USD’, p_planetype = ‘DOM’ ) INTO TABLE @DATA(lt_act_res).

cl_abap_unit_assert=>assert_equals(
EXPORTING
act = lines( lt_act_res )
exp = lines( lt_exp_res )
msg = |No. of lines must be 2| ).
ENDMETHOD.

ENDCLASS.


But some times the base CDS may involve multiple tables, but if intended to test a specific table details on which the CDS view is build on, the pass the dependency list that are intended for testing.

8


 

One comment

  1. ‘Testing of a CDS hierarchy is not supported’ – I get this error message when I try to execute the steps you have mentioned.

    Like

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