RAP Aunit Testing for business object.
Introduction:
In this blog post, Iam performing unit testing for simple RAP business object for create entity operation. This blog is particularly for absolute beginners who would like to start systematic Unit Testing in RAP.
testing the behavior of a unit of business object without depending on external objects like databases etc.
Requirement:
We need a Global variable to maintain our test environment for CDS view of Business object V_ENVIRONMENT with reference IF_CDS_TEST_ENVIRONMENT.
CLASS -DATA: v_environment TYPE REF TO if_cds_test_environment.
We configure the environment and assign the views and tables that require test doubles.
v_environment = cl_cds_test_environment=>create(
i_for_entity = ‘ZKAL_DD_FLIGHT_AUNIT’
i_dependency_list = VALUE #( ( name = ‘ZKAL_DB_FLIGHT’ type = ‘TABLE’ ) )
).
Here
ZKAL_DD_FLIGHT_AUNIT is CDS view,
ZKAL_DB_FLIGHT is a Database table,
cl_cds_test_environment is the standard class.
We acquire the double we want to configure by specifying the CDS view name.
Create a RAP Application based on your requirements.
CDS View for A UNIT Test
next create =>projection view => service definition => service binding => Preview
Create one global class,
Next click on option Test classes below.
Then type test then insert template of test classes
Then give appropriate name for the test class (it should be unique from the global class name)
CLASS ltcl_aunit_rap DEFINITION DEFERRED.
CLASS zcl_kal_aunit_testing_rap DEFINITION LOCAL FRIENDS ltcl_aunit_rap.
CLASS ltcl_aunit_rap DEFINITION
FINAL FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
CLASS-DATA:
v_environment TYPE REF TO if_cds_test_environment.
CLASS-METHODS:
class_setup RAISING cx_static_check,
class_teardown.
METHODS:
create_new_entry FOR TESTING,
setup,
Teardown.
ENDCLASS.
Click ctrl+1 at method then click on add implementation.
Implementation For method class-setup.
Static method. Executes once for the test class.
METHOD class_setup.
DATA : lt_flight TYPE STANDARD TABLE OF zkal_db_flight WITH EMPTY KEY.
v_environment = cl_cds_test_environment=>create(
i_for_entity = ‘ZKAL_DD_FLIGHT_AUNIT’
i_dependency_list = VALUE #( ( name = ‘ZKAL_DB_FLIGHT’ type = ‘TABLE’ ) )
).
lt_FLIGHT = VALUE #( ( carrier_id = ‘AA’
connection_id = ’34’
currency_code = ‘USD’
flight_date = ‘20250205’
plane_type_id = ‘767-200’
price = ‘2122’
seats_max = ‘229’
seats_occupied = ‘113’ ) ).
v_environment->insert_test_data( lt_flight ).
v_environment->enable_double_redirection( ).
ENDMETHOD.
Implementation for class teardown method.
To destroy the environment, we should call instance method using object.
METHOD class_teardown.
v_environment->destroy( ).
ENDMETHOD.
Implementation for setup method.
Clear_double method will Clears the data in the test doubles Use this method in AUnit setup method to clear all the doubles before each method execution.
METHOD setup.
v_environment->clear_doubles( ).
ENDMETHOD.
Implementation of the Teardown method.
The ROLLBACK ENTITIES statement resets the transactional buffer. It is used outside of behavior pools to roll back all changes done since the last COMMIT ENTITIES operation.
METHOD teardown.
ROLLBACK ENTITIES.
ENDMETHOD
Implementation of the create_new_entry
METHOD create_new_entry.
DATA:
lt_new_flight TYPE TABLE FOR CREATE zkal_dd_flight_aunit.
lt_new_flight = VALUE #( ( CarrierId = ‘AA’
ConnectionId = ’34’
CurrencyCode = ‘USD’
FlightDate = ‘20250205’
PlaneTypeId = ‘767-200’
Price = ‘2122’
SeatsMax = ‘229’
SeatsOccupied = ‘113’
%cid = ’01’
%control-CarrierId = if_abap_behv=>mk-on
%control-ConnectionId = if_abap_behv=>mk-on
%control-CurrencyCode = if_abap_behv=>mk-on
%control-FlightDate = if_abap_behv=>mk-on
%control-PlaneTypeId = if_abap_behv=>mk-on
%control-Price = if_abap_behv=>mk-on
%control-SeatsMax = if_abap_behv=>mk-on
%control-SeatsOccupied = if_abap_behv=>mk-on
) ).
MODIFY ENTITY zkal_dd_flight_aunit
CREATE FROM lt_new_flight
MAPPED DATA(ls_mapped)
REPORTED DATA(ls_commit_reported)
FAILED DATA(ls_commit_failed).
COMMIT ENTITIES .
SELECT SINGLE FROM zkal_db_flight
FIELDS carrier_id, connection_id, flight_date
WHERE carrier_id = ‘AA’
INTO @DATA(ls_flight_found).
cl_abap_unit_assert=>assert_not_initial( msg = ‘Flight from db’
act = ls_flight_found ).
” assert the generation carrier ID (key) at creation
cl_abap_unit_assert=>assert_not_initial( msg = ‘Carrier-id’
act = ls_flight_found-carrier_id ).
” assert Connection id
cl_abap_unit_assert=>assert_equals( msg = ‘Connection id’ exp = ‘AA’
act = ls_flight_found-connection_id ).
” assert the flight date
cl_abap_unit_assert=>assert_equals( msg = ‘Flight date’ exp = ’16’
act = ls_flight_found-flight_date ).
cl_abap_unit_assert=>assert_subrc( ).
ENDMETHOD.
Overall code.
CLASS ltcl_aunit_rap DEFINITION DEFERRED.
CLASS zcl_kal_aunit_testing_rap DEFINITION LOCAL FRIENDS ltcl_aunit_rap.
CLASS ltcl_aunit_rap DEFINITION
FINAL FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
CLASS-DATA:v_environment TYPE REF TO if_cds_test_environment.
CLASS-METHODS: class_setup RAISING cx_static_check,
class_teardown.
METHODS: create_new_entry FOR TESTING,
setup,
teardown.
ENDCLASS.
“Implementation
CLASS ltcl_aunit_rap IMPLEMENTATION.
METHOD class_setup.
DATA : lt_flight TYPE STANDARD TABLE OF zkal_db_flight WITH EMPTY KEY.
v_environment = cl_cds_test_environment=>create(
i_for_entity = ‘ZKAL_DD_FLIGHT_AUNIT’
i_dependency_list = VALUE #( ( name = ‘ZKAL_DB_FLIGHT’ type = ‘TABLE’ ) )
).
lt_FLIGHT = VALUE #( ( carrier_id = ‘AA’
connection_id = ’34’
currency_code = ‘USD’
flight_date = ‘20250205’
plane_type_id = ‘767-200’
price = ‘2122’
seats_max = ‘229’
seats_occupied = ‘113’ ) ).
v_environment->insert_test_data( lt_flight ).
v_environment->enable_double_redirection( ).
ENDMETHOD.
METHOD class_teardown.
v_environment->destroy( ).
ENDMETHOD.
METHOD create_new_entry.
DATA:lt_new_flight TYPE TABLE FOR CREATE zkal_dd_flight_aunit.
lt_new_flight = VALUE #( ( CarrierId = ‘AA’
ConnectionId = ’34’
CurrencyCode = ‘USD’
FlightDate = ‘20250205’
PlaneTypeId = ‘767-200’
Price = ‘2122’
SeatsMax = ‘229’
SeatsOccupied = ‘113’
%cid = ’01’
%control-CarrierId = if_abap_behv=>mk-on
%control-ConnectionId = if_abap_behv=>mk-on
%control-CurrencyCode = if_abap_behv=>mk-on
%control-FlightDate = if_abap_behv=>mk-on
%control-PlaneTypeId = if_abap_behv=>mk-on
%control-Price = if_abap_behv=>mk-on
%control-SeatsMax = if_abap_behv=>mk-on
%control-SeatsOccupied = if_abap_behv=>mk-on
) ).
MODIFY ENTITY zkal_dd_flight_aunit
CREATE FROM lt_new_flight
MAPPED DATA(ls_mapped)
REPORTED DATA(ls_commit_reported)
FAILED DATA(ls_commit_failed).
COMMIT ENTITIES .
SELECT SINGLE FROM zkal_db_flight
FIELDS carrier_id, connection_id, flight_date
WHERE carrier_id = ‘AA’
INTO @DATA(ls_flight_found).
cl_abap_unit_assert=>assert_not_initial( msg = ‘Flight from db’
act = ls_flight_found ).
” assert the generation carrier ID (key) at creation
cl_abap_unit_assert=>assert_not_initial( msg = ‘Carrier-id’
act = ls_flight_found-carrier_id ).
” assert that the action has changed the Connection id
cl_abap_unit_assert=>assert_equals( msg = ‘Connection id’ exp = ‘A’
act = ls_flight_found-connection_id ).
” assert the discounted flight date
cl_abap_unit_assert=>assert_equals( msg = ‘Flight date’ exp = ’16’
act = ls_flight_found-flight_date ).
cl_abap_unit_assert=>assert_subrc( ).
ENDMETHOD.
METHOD setup.
v_environment->clear_doubles( ).
ENDMETHOD.
METHOD teardown.
ROLLBACK ENTITIES.
ENDMETHOD.
ENDCLASS.
click ctrl+shift+f12
double click on ABAP traces to get graph
RAP Aunit Testing for business object. Introduction: In this blog post, Iam performing unit testing for simple RAP business object for create entity operation. This blog is particularly for absolute beginners who would like to start systematic Unit Testing in RAP. testing the behavior of a unit of business object without depending on external objects like databases etc. Requirement: We need a Global variable to maintain our test environment for CDS view of Business object V_ENVIRONMENT with reference IF_CDS_TEST_ENVIRONMENT. CLASS -DATA: v_environment TYPE REF TO if_cds_test_environment. We configure the environment and assign the views and tables that require test doubles. v_environment = cl_cds_test_environment=>create( i_for_entity = ‘ZKAL_DD_FLIGHT_AUNIT’ i_dependency_list = VALUE #( ( name = ‘ZKAL_DB_FLIGHT’ type = ‘TABLE’ ) ) ). Here ZKAL_DD_FLIGHT_AUNIT is CDS view, ZKAL_DB_FLIGHT is a Database table, cl_cds_test_environment is the standard class. We acquire the double we want to configure by specifying the CDS view name. Create a RAP Application based on your requirements. CDS View for A UNIT Test next create =>projection view => service definition => service binding => Preview Create one global class, Next click on option Test classes below. Then type test then insert template of test classes Then give appropriate name for the test class (it should be unique from the global class name) CLASS ltcl_aunit_rap DEFINITION DEFERRED.
CLASS zcl_kal_aunit_testing_rap DEFINITION LOCAL FRIENDS ltcl_aunit_rap.
CLASS ltcl_aunit_rap DEFINITION
FINAL FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
CLASS-DATA:
v_environment TYPE REF TO if_cds_test_environment.
CLASS-METHODS:
class_setup RAISING cx_static_check,
class_teardown.
METHODS:
create_new_entry FOR TESTING,
setup,
Teardown.
ENDCLASS. Click ctrl+1 at method then click on add implementation. Implementation For method class-setup. Static method. Executes once for the test class. METHOD class_setup.
DATA : lt_flight TYPE STANDARD TABLE OF zkal_db_flight WITH EMPTY KEY.
v_environment = cl_cds_test_environment=>create(
i_for_entity = ‘ZKAL_DD_FLIGHT_AUNIT’
i_dependency_list = VALUE #( ( name = ‘ZKAL_DB_FLIGHT’ type = ‘TABLE’ ) )
).
lt_FLIGHT = VALUE #( ( carrier_id = ‘AA’
connection_id = ’34’
currency_code = ‘USD’
flight_date = ‘20250205’
plane_type_id = ‘767-200’
price = ‘2122’
seats_max = ‘229’
seats_occupied = ‘113’ ) ).
v_environment->insert_test_data( lt_flight ).
v_environment->enable_double_redirection( ).
ENDMETHOD. Implementation for class teardown method. To destroy the environment, we should call instance method using object. METHOD class_teardown.
v_environment->destroy( ).
ENDMETHOD. Implementation for setup method. Clear_double method will Clears the data in the test doubles Use this method in AUnit setup method to clear all the doubles before each method execution. METHOD setup.
v_environment->clear_doubles( ).
ENDMETHOD. Implementation of the Teardown method. The ROLLBACK ENTITIES statement resets the transactional buffer. It is used outside of behavior pools to roll back all changes done since the last COMMIT ENTITIES operation. METHOD teardown.
ROLLBACK ENTITIES.
ENDMETHOD Implementation of the create_new_entry METHOD create_new_entry.
DATA:
lt_new_flight TYPE TABLE FOR CREATE zkal_dd_flight_aunit.
lt_new_flight = VALUE #( ( CarrierId = ‘AA’
ConnectionId = ’34’
CurrencyCode = ‘USD’
FlightDate = ‘20250205’
PlaneTypeId = ‘767-200’
Price = ‘2122’
SeatsMax = ‘229’
SeatsOccupied = ‘113’
%cid = ’01’
%control-CarrierId = if_abap_behv=>mk-on
%control-ConnectionId = if_abap_behv=>mk-on
%control-CurrencyCode = if_abap_behv=>mk-on
%control-FlightDate = if_abap_behv=>mk-on
%control-PlaneTypeId = if_abap_behv=>mk-on
%control-Price = if_abap_behv=>mk-on
%control-SeatsMax = if_abap_behv=>mk-on
%control-SeatsOccupied = if_abap_behv=>mk-on
) ).
MODIFY ENTITY zkal_dd_flight_aunit
CREATE FROM lt_new_flight
MAPPED DATA(ls_mapped)
REPORTED DATA(ls_commit_reported)
FAILED DATA(ls_commit_failed).
COMMIT ENTITIES .
SELECT SINGLE FROM zkal_db_flight
FIELDS carrier_id, connection_id, flight_date
WHERE carrier_id = ‘AA’
INTO @DATA(ls_flight_found).
cl_abap_unit_assert=>assert_not_initial( msg = ‘Flight from db’
act = ls_flight_found ).
” assert the generation carrier ID (key) at creation
cl_abap_unit_assert=>assert_not_initial( msg = ‘Carrier-id’
act = ls_flight_found-carrier_id ).
” assert Connection id
cl_abap_unit_assert=>assert_equals( msg = ‘Connection id’ exp = ‘AA’
act = ls_flight_found-connection_id ).
” assert the flight date
cl_abap_unit_assert=>assert_equals( msg = ‘Flight date’ exp = ’16’
act = ls_flight_found-flight_date ).
cl_abap_unit_assert=>assert_subrc( ).
ENDMETHOD. Overall code. CLASS ltcl_aunit_rap DEFINITION DEFERRED.
CLASS zcl_kal_aunit_testing_rap DEFINITION LOCAL FRIENDS ltcl_aunit_rap.
CLASS ltcl_aunit_rap DEFINITION
FINAL FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
CLASS-DATA:v_environment TYPE REF TO if_cds_test_environment.
CLASS-METHODS: class_setup RAISING cx_static_check,
class_teardown.
METHODS: create_new_entry FOR TESTING,
setup,
teardown.
ENDCLASS.
“Implementation
CLASS ltcl_aunit_rap IMPLEMENTATION.
METHOD class_setup.
DATA : lt_flight TYPE STANDARD TABLE OF zkal_db_flight WITH EMPTY KEY.
v_environment = cl_cds_test_environment=>create(
i_for_entity = ‘ZKAL_DD_FLIGHT_AUNIT’
i_dependency_list = VALUE #( ( name = ‘ZKAL_DB_FLIGHT’ type = ‘TABLE’ ) )
).
lt_FLIGHT = VALUE #( ( carrier_id = ‘AA’
connection_id = ’34’
currency_code = ‘USD’
flight_date = ‘20250205’
plane_type_id = ‘767-200’
price = ‘2122’
seats_max = ‘229’
seats_occupied = ‘113’ ) ).
v_environment->insert_test_data( lt_flight ).
v_environment->enable_double_redirection( ).
ENDMETHOD.
METHOD class_teardown.
v_environment->destroy( ).
ENDMETHOD.
METHOD create_new_entry.
DATA:lt_new_flight TYPE TABLE FOR CREATE zkal_dd_flight_aunit.
lt_new_flight = VALUE #( ( CarrierId = ‘AA’
ConnectionId = ’34’
CurrencyCode = ‘USD’
FlightDate = ‘20250205’
PlaneTypeId = ‘767-200’
Price = ‘2122’
SeatsMax = ‘229’
SeatsOccupied = ‘113’
%cid = ’01’
%control-CarrierId = if_abap_behv=>mk-on
%control-ConnectionId = if_abap_behv=>mk-on
%control-CurrencyCode = if_abap_behv=>mk-on
%control-FlightDate = if_abap_behv=>mk-on
%control-PlaneTypeId = if_abap_behv=>mk-on
%control-Price = if_abap_behv=>mk-on
%control-SeatsMax = if_abap_behv=>mk-on
%control-SeatsOccupied = if_abap_behv=>mk-on
) ).
MODIFY ENTITY zkal_dd_flight_aunit
CREATE FROM lt_new_flight
MAPPED DATA(ls_mapped)
REPORTED DATA(ls_commit_reported)
FAILED DATA(ls_commit_failed).
COMMIT ENTITIES .
SELECT SINGLE FROM zkal_db_flight
FIELDS carrier_id, connection_id, flight_date
WHERE carrier_id = ‘AA’
INTO @DATA(ls_flight_found).
cl_abap_unit_assert=>assert_not_initial( msg = ‘Flight from db’
act = ls_flight_found ).
” assert the generation carrier ID (key) at creation
cl_abap_unit_assert=>assert_not_initial( msg = ‘Carrier-id’
act = ls_flight_found-carrier_id ).
” assert that the action has changed the Connection id
cl_abap_unit_assert=>assert_equals( msg = ‘Connection id’ exp = ‘A’
act = ls_flight_found-connection_id ).
” assert the discounted flight date
cl_abap_unit_assert=>assert_equals( msg = ‘Flight date’ exp = ’16’
act = ls_flight_found-flight_date ).
cl_abap_unit_assert=>assert_subrc( ).
ENDMETHOD.
METHOD setup.
v_environment->clear_doubles( ).
ENDMETHOD.
METHOD teardown.
ROLLBACK ENTITIES.
ENDMETHOD.
ENDCLASS. click ctrl+shift+f12double click on ABAP traces to get graph Read More Application Development Blog Posts articles
#SAP
+ There are no comments
Add yours