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:
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的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2723615/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SAP WebClient UI component模型後設資料解析工具WebclientUI模型
- 使用Selenium自動化測試SAP CRM WebClient UIWebclientUI
- SAP CRM WebClient UI cross component跳轉的一個具體例子WebclientUIROS
- SAP WebClient UI One Hit Navigation的實現方法WebclientUINavigation
- SAP 電商雲 Spartacus UI SSR 單元測試裡的 callFakeUI
- SAP CRM訂單模型CRMD_SHIPPING的單元測試方法模型
- SAP WebClient UI drop down list(下拉選單)的一個故障和解決方法WebclientUI
- iOS 單元測試和 UI 測試快速入門iOSUI
- 使用note++開發SAP WebClient UIWebclientUI
- SAP WebClient UI的白屏問題分析WebclientUI
- 如何在SAP WebClient UI裡使用jChartFXWebclientUI
- SAP CRM WebClient UI和Fiori UI混搭並存WebclientUI
- 單元測試如何測試私有方法_1
- 測試 之Java單元測試、Android單元測試JavaAndroid
- SAP WebClient UI的會話重啟原理WebclientUI會話
- SAP 電商雲 Spartacus UI 的單元測試和端到端測試,以及 CI/CD 相關話題UI
- SAP Spartacus OccEndpointsService單元測試的依賴注入依賴注入
- 單元測試:單元測試中的mockMock
- SAP UI5 初學者教程之二十七 - SAP UI5 應用的單元測試工具 QUnit 介紹試讀版UI
- SAP CRM WebClient UI和Hybris backoffice UI開發的相同點WebclientUI
- 在SAP WebClient UI裡顯示倒數計時的UIWebclientUI
- 如何單元測試Java的private方法Java
- 如何在SAP WebClient UI裡使用HANA Live reportWebclientUI
- SAP CDS view單元測試框架Test Double介紹View框架
- 在SAP CRM WebClient UI中用javascript觸發ABAP eventWebclientUIJavaScript
- SAP CRM WebClient UI異常的持久化機制WebclientUI持久化
- 將SAP CRM WebClient UI的表格匯出成PDFWebclientUI
- 自己實現一個SAP WebClient UI Repository Information SystemWebclientUIORM
- 如何將SAP WebClient UI的表格匯出成PDFWebclientUI
- 單元測試時靜態方法注意點
- SAP 電商雲 Spartacus UI Angular Component 動態建立的單步除錯UIAngular除錯
- 三種動態控制SAP CRM WebClient UI assignment block顯示與否的方法WebclientUIBloC
- 單元測試,只是測試嗎?
- SAP UI5 sap.ui.Device.media.initRangeSet 方法的單步除錯UIdev除錯
- 單元測試-【轉】論單元測試的重要性
- SAP UI5 Web Component React應用如何在Component之間跳轉UIWebReact
- <context:component-scan> 標籤Context
- SpringBoot單元測試Spring Boot