CDS View Unit Test with Association

light111CDS View Unit Test with Association


The below is a CDS view on table SPFLI with association to SCARR table.

1

Data preview and then association navigation.

23

Class with local test class.

910

During creation of environment pass TEST_ASSOCIATION as true.

45


67


8


use this source file for your ABAP unit test classes
CLASS lcl_demo_flight_asso_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.
METHODS: test_flight_asso FOR TESTING.

ENDCLASS.
CLASS lcl_demo_flight_asso_test IMPLEMENTATION.
METHOD class_setup.
cl_cds_test_environment=>create(
EXPORTING
i_for_entity = ‘ZDEMO_FLIGHT’       ” CDS VIEW NAME
test_associations = abap_true         ”  want to test the association
RECEIVING
r_result = lo_enviro ).
ENDMETHOD.

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

METHOD setup.

* insert test data into component
DATA: lt_spfli TYPE TABLE OF spfli.
lt_spfli = VALUE #(
( mandt = sy-mandt carrid = ‘A1’ connid = ‘0001’ countryfr = ‘US’ countryto = ‘IN’ )
( mandt = sy-mandt carrid = ‘A1’ connid = ‘0002’ countryfr = ‘UK’ countryto = ‘IN’ )
( mandt = sy-mandt carrid = ‘A2’ connid = ‘0001’ countryfr = ‘US’ countryto = ‘IN’ )
( mandt = sy-mandt carrid = ‘A2’ connid = ‘0002’ countryfr = ‘UK’ countryto = ‘IN’ )
).
* insert the test data
lo_enviro->insert_test_data( EXPORTING i_data = lt_spfli ).
* insert test data into component association
DATA: lt_scarr TYPE TABLE OF scarr.
lt_scarr = VALUE #(
( mandt = sy-mandt carrid = ‘A1’ carrname = ‘Americal Airline1’ currcode = ‘USD’ url = ” )
( mandt = sy-mandt carrid = ‘A2’ carrname = ‘Americal Airline2’ currcode = ‘USD’ url = ” )
).
* insert the test data
lo_enviro->insert_test_data( EXPORTING i_data = lt_scarr ).

ENDMETHOD.

METHOD teardown.
lo_enviro->clear_doubles( ).
ENDMETHOD.
METHOD test_flight.
DATA: lt_exp_res TYPE TABLE OF zdemo_flight.
lt_exp_res = VALUE #(
( carrid = ‘A1’ connid = ‘0001’ countryfr = ‘US’ countryto = ‘IN’ )
( carrid = ‘A1’ connid = ‘0002’ countryfr = ‘UK’ countryto = ‘IN’ )
( carrid = ‘A2’ connid = ‘0001’ countryfr = ‘US’ countryto = ‘IN’ )
( carrid = ‘A2’ connid = ‘0002’ countryfr = ‘UK’ countryto = ‘IN’ )
).

” select on cds view
SELECT * FROM zdemo_flight 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 4| ).
ENDMETHOD.

METHOD test_flight_asso.
DATA: lt_exp_res TYPE TABLE OF scarr.
lt_exp_res = VALUE #(
( carrid = ‘A1’ carrname = ‘Americal Airline1’ currcode = ‘USD’ url = ” )
( carrid = ‘A2’ carrname = ‘Americal Airline2’ currcode = ‘USD’ url = ” )
).

” select on cds view association
SELECT * FROM zdemo_flight_scarr INTO TABLE @DATA(lt_act_res).

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

ENDCLASS.


 

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