CDS View Unit Test for Multiple Views

light111CDS View Unit Test for multiple views


Below we have 2 CDS views.

1

Data preview.

11

2

Data preview.

12

Create a class and its local test class.

34

While creating the doubles environment, the method CREATE_FOR_MULTIPLE_CDS can be used and all the CDS view names can be passed.

5678910


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

ENDCLASS.
CLASS lcl_demo_flight_test IMPLEMENTATION.
METHOD class_setup.
lo_enviro = cl_cds_test_environment=>create_for_multiple_cds(

i_for_entities = VALUE #(
( i_for_entity = ‘ZDEMO_SCARR’ )      ” cds view name
( i_for_entity = ‘ZDEMO_SPFLI’ )       ” cds view name
) ).
ENDMETHOD.

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

METHOD setup.

* insert test data ifor scarr and spfli – dependent components
DATA: lt_scarr TYPE TABLE OF scarr.
DATA: lt_spfli TYPE TABLE OF spfli.
lt_scarr = VALUE #(
( mandt = sy-mandt carrid = ‘A1’ carrname = ‘American Airline’ currcode = ‘USD’ url = ” )
( mandt = sy-mandt carrid = ‘B1’ carrname = ‘British Airline’ currcode = ‘EUR’ url = ” )
).

lt_spfli = VALUE #(
( mandt = sy-mandt carrid = ‘A1’ connid = ‘0001’ countryfr = ‘US’ countryto = ‘IN’ cityfrom = ” cityto = ” airpfrom = ” airpto = ” )
( mandt = sy-mandt carrid = ‘B1’ connid = ‘0001’ countryfr = ‘UK’ countryto = ‘IN’ cityfrom = ” cityto = ” airpfrom = ” airpto = ” )
).
* insert the test data
lo_enviro->insert_test_data( EXPORTING i_data = lt_scarr ).
lo_enviro->insert_test_data( EXPORTING i_data = lt_spfli ).

ENDMETHOD.

METHOD teardown.
lo_enviro->clear_doubles( ).
ENDMETHOD.
METHOD test_flight1_data.
DATA: lt_exp_res TYPE TABLE OF zdemo_scarr.
lt_exp_res = VALUE #(
( carrid = ‘A1’ carrname = ‘American Airline’ currcode = ‘USD’ url = ” )
( carrid = ‘B1’ carrname = ‘British Airline’ currcode = ‘EUR’ url = ” )
).

” select on cds view
SELECT * FROM zdemo_scarr 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.

METHOD test_flight2_data.
DATA: lt_exp_res TYPE TABLE OF zdemo_spfli.
lt_exp_res = VALUE #(
( carrid = ‘A1’ connid = ‘0001’ countryfr = ‘US’ countryto = ‘IN’ )
( carrid = ‘B1’ connid = ‘0001’ countryfr = ‘UK’ countryto = ‘IN’ )
).

” select on cds view
SELECT * FROM zdemo_spfli 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.


 

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