zoukankan      html  css  js  c++  java
  • 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的原创文章,请关注公众号"汪子熙":

  • 相关阅读:
    MyEclipse控制台输出tomcat红字
    struts标签bean:define
    字节流转换为对象的方法
    C#将jpg格式图片合成到bmp格式图片中
    [置顶]C#中Socket服务端代码分享
    [置顶] C#中Socket服务端代码分享
    [置顶] 使用Joson的格式字符串在Socket中通讯时数据格式的转换
    [置顶] AMF序列化为对象和AMF序列化为二进制字节流
    用C#获取CPU编号、硬盘编号等系统有关环境、属性
    别把紧张情绪带回家 下班后的10个最佳放松法
  • 原文地址:https://www.cnblogs.com/sap-jerry/p/13549298.html
Copyright © 2011-2022 走看看