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:
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:
Here below is automatically generated code:
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.
First attempt
When performing unit test for the first time, the statement coverage is extremely low:
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.
Solution
I plan to pass a fake bol entity for parameter FOCUS_BO. Then I construct it in method class_setup:
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:
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.
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.
Those unreached statements are marked as red. The reason is there is no entity contained in collection wrapper.
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:
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关推荐
SAP CRM WebClient UI 培训 Slide
CRM7.0 WebClient UI config
这是cross component navigation在起作用。 Result list是model在ICQR里面,如果点service,会navigate到service detail page,这个service detail page是做在PRD02OV上,所以这个navigation是cross component ...
Sent: Thursday, May 9, 2013 6:30 PM Subject: ON_NEW_FOCUS的用途 ROOT ———————————— Product ID PRODshortText ———————————— Product Description Genil Model hierarchy
UI Guidelines for CRM WebClient User Interface
C# WebClient 上传文件 WebClient上传文件至服务器
WEBCLIENT使用方法
例1:使用WebClient中的UploadFile方法上传文件。代码如下。使用此方法需要将上传的文件夹权限设置为 IIS来宾账户允许读写。 例2:使用WebClient的UpLoadData上传文件 例3:使用WebClient的Write方法上传。 例4:...
WebClient jar包
webui学习者
C# WebClient实现文件下载 示例源码
一个基于node.js和socket.io的简单IRC-Webclient。 因此,它将websockets用于node.js服务器与浏览器之间的通信。 大多数功能都实现为在客户端运行。 这样可以确保用户只有在尝试修改客户端时才能破坏自己的客户端...
使用WebClient的UploadFileAsync、UploadFile、DownloadFile、DownloadFileAsync方法,异步调用服务器上传下载数据。
c#中的WebClient的用法,模拟请求
本篇文章主要介绍了C#中WebClient实现文件下载,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
支持 Session 和 Cookie 的 WebClient
WebClient下载文件展示进度条
webClient 服务器端与客户端配置
WebClient的例子,在Eclipse中直接可以运行
C# WEBClient 文件上传下载