SAP WebClient UI component context node class单元测试方法

Prerequisite for reading this blog: you should be familiar with how to work with ABAP unit test framework.
Requirement is to write unit test for this method below:

SAP WebClient UI component context node class单元测试方法_第1张图片

In this blog, I copy the standard class CL_PRDTXT_TEXTCUCO_CN02 into a local class ZCL_PRDTXT_TEXTCUCO_CN02 and generate test class based on the latter. The same approach could be applied to the standard class for sure.
Create local unit test class for it via wizard:

SAP WebClient UI component context node class单元测试方法_第2张图片

Here below is automatically generated code:

SAP WebClient UI component context node class单元测试方法_第3张图片

Since usually we will manipulate private attributes of CUT ( class under test ) so insert the following code into header part of local test class, so that local test class can modify private attributes of CUT.

SAP WebClient UI component context node class单元测试方法_第4张图片

First attempt

When performing unit test for the first time, the statement coverage is extremely low:

SAP WebClient UI component context node class单元测试方法_第5张图片

Check the source code of tested method, the reason is in our local test class, we didn’t pass a valid value for parameter FOCUS_BO, so the method directly return when its first line is executed.

SAP WebClient UI component context node class单元测试方法_第6张图片

Solution

I plan to pass a fake bol entity for parameter FOCUS_BO. Then I construct it in method class_setup:

SAP WebClient UI component context node class单元测试方法_第7张图片

METHOD class_setup.
    lo_prod = zcl_prod_unit_test_tool=>get_fake_bol_entity(
       iv_bol_name = 'Product'
       is_data = get_sample_data( )
       iv_key = get_sample_data( )-product_guid ).

  ENDMETHOD.

METHOD get_sample_data.
    rs_data = VALUE #( product_guid = '0123456789123456' product_id = 'I042416' product_type = '01' ).
  ENDMETHOD.
Now the local test class method is changed as below:
METHOD on_new_focus.
    DATA focus_bo TYPE REF TO if_bol_bo_property_access.

    focus_bo ?= lo_prod.
    f_cut->on_new_focus( focus_bo ).
  ENDMETHOD.

Second attempt

When I run unit test, it fails this time with following error message:

SAP WebClient UI component context node class单元测试方法_第8张图片

The reason is in line 42, the method being tested tries to read product with a valid guid, unfortunately in my test code I have passed a fake guid ‘0123456789123456’, thus not_found exception is raised.

SAP WebClient UI component context node class单元测试方法_第9张图片

Solution

Change get_sample_data in local test class as below, which can ensure the product_guid is always valid since it is read from DB table.

METHOD get_sample_data.
    DATA:ls_prod TYPE comm_product.
    SELECT SINGLE * INTO ls_prod FROM comm_product WHERE product_type = '01'.
    rs_data = VALUE #( product_guid = ls_prod-product_guid product_id = ls_prod-product_id product_type = '01' ).
  ENDMETHOD.

Third attempt

Now the unit test could pass successfully, however still some statement is not executed at all.

SAP WebClient UI component context node class单元测试方法_第10张图片

Those unreached statements are marked as red. The reason is there is no entity contained in collection wrapper.

SAP WebClient UI component context node class单元测试方法_第11张图片

Solution

Create a new method in test class:

METHOD create_wrapper.
    DATA: lr_attr TYPE REF TO crmst_uiu_text_attr.

    CREATE DATA lr_attr.

    DATA(lr_value) = NEW cl_bsp_wd_value_node( lr_attr ).
    CREATE OBJECT f_cut->collection_wrapper.

    f_cut->collection_wrapper->add( lr_value ).
  ENDMETHOD.
And call it in set_up method:
METHOD setup.
    CREATE OBJECT f_cut.
    create_wrapper( ).
  ENDMETHOD.

Finally, the unit test is finished successfully and all executable statements have been covered:

SAP WebClient UI component context node class单元测试方法_第12张图片

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

你可能感兴趣的:(sap,saprfc,crm,erp,abap)