1. 程式人生 > 實用技巧 >SAP CL_CRM_BOL_ENTITY單元測試方法

SAP CL_CRM_BOL_ENTITY單元測試方法

In SAP standard development it is very important to use unit test to cover those changes on legacy code to try to avoid side effect. As type reference CL_CRM_BOL_ENTITY is widely used in UI component code, I would like to find a way to construct its reference by sample data.

The normal way we get instance of CL_CRM_BOL_ENTITY is, to use query or dquery method provided in class CL_CRM_BOL_CORE, which return a BOL collection list and then we can get each bol entity from by iterating that collection. However, both of these methods will perform database scan and return found data – such behavior is not appropriate for unit test.
I have gone through all public methods in CL_CRM_BOL_CORE and do not find a useful method to construct bol entity based on sample data, so I write a small piece of code:

I create a class which inherits CL_CRM_BOL_ENTITY:

Create a static private attribute:

And initialize it in class_constructor:

METHOD class_constructor.
so_bol_core = cl_crm_bol_core=>get_instance( ).
so_bol_core->load_component_set( 'PROD_ALL' ).
ENDMETHOD.

The signature and source code are listed below:

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_PROD_UNIT_TEST_TOOL=>GET_FAKE_BOL_ENTITY
* +-------------------------------------------------------------------------------------------------+
* | [--->] IS_DATA TYPE ANY
* | [--->] IV_BOL_NAME TYPE CRMT_EXT_OBJ_NAME
* | [--->] IV_KEY TYPE CRMT_OBJECT_GUID
* | [<-()] RO_RESULT TYPE REF TO CL_CRM_BOL_ENTITY
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD get_fake_bol_entity.
DATA: lr_cast TYPE REF TO cl_crm_genil_container_object,
lr_entity TYPE REF TO crmt_bol_entity_line.

DATA(lv_root_list) = cl_crm_genil_container_tools=>get_new_cont_root_list( ).

DATA(lr_root_object) = lv_root_list->add_object(
iv_object_name = iv_bol_name
is_object_key = iv_key
iv_attr_req = abap_false ).

lr_root_object->set_attributes( is_data ).

lr_cast ?= lr_root_object.
CREATE DATA lr_entity.
lr_entity->instance = lr_cast->data_ref->instance.
ro_result = NEW cl_crm_bol_entity( iv_cont_proxy = lr_cast
iv_manager_entry = lr_entity ).
ENDMETHOD.
And below is an example how to construct BOL entity using this utility method:
CONSTANTS: cv_prod_guid TYPE crmt_object_guid VALUE '0123456789123456'.
DATA(ls_prod_header) = VALUE comt_product_ui( product_guid = cv_prod_guid
product_id = 'I042416' product_type = '01' ).


DATA(ro_entity) = zcl_prod_unit_test_tool=>get_fake_bol_entity(
iv_bol_name = 'Product'
is_data = ls_prod_header
iv_key = cv_prod_guid ).

DATA(lv_type) = ro_entity->get_property_as_string( 'PRODUCT_TYPE' ).
DATA(lv_guid) = ro_entity->get_property_as_string( 'PRODUCT_GUID' ).
DATA(lv_id) = ro_entity->get_property_as_string( 'PRODUCT_ID' ).

WRITE: / lv_type.
WRITE: / lv_guid.
WRITE: / lv_id.

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":