使用SAP CRM mock框架進行單元測試的設計
There is an interface IF_CRM_PRODUCT_PROXY in CRM which declares almost all function modules from function group COM_PRODUCT_API for product master access.
This interface is implemented by three classes below.
CL_CRM_PRODUCT_PROXY will delegate the call to MOCK_PROXY( for unit test ) or REAL_PROXY( for productive use ) according to different mode in which the code is running. As an application developer, we do not directly call CL_CRM_PRODUCT_PROXY in our application code, but use another wrapper class CL_CRM_PRODUCT_API instead. For example, I would like to get the product guid for product with id “I042416”, I use the following source code:
In productive use scenario, the callstack could be found below: our test report calls CL_CRM_PRODUCT_API, which calls CL_CRM_PRODUCT_PROXY, which then calls the productive implementation – CL_CRM_PRODUCT_REAL_PROXY. Inside the real proxy, function module COM_PRODUCT_GUID_GET is called.
How to use CRM mock framework to make fake product guid is returned
Suppose in my unit test code, I would like a fake guid “AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA” is returned by the same source code. 1. Just add the following source code to let CL_CRM_PRODUCT_PROXY know that currently the code is running under mock mode.
cl_factory_proxy=>set_source( iv_source = 'MOCK'
iv_xml_master_data_file = '\\TSHomeServer\TSHome$\i042416\Desktop\test.xml'
iv_read_from_mime = abap_false ).
This code would probably be inserted into the class_setup of your unit test code.
- paste the following source code into your test.xml:
<Mock XMLCompressVersion="2">
<XMLRC_Object ObjectName="CL_CRM_PRODUCT_API" Component="COM_PRODUCT_GUID_GET" input="8B70F13430313231340300">
<EV_PRODUCT_GUID>qqqqqqqqqqqqqqqqqqqqqg==</EV_PRODUCT_GUID></XMLRC_Object></Mock>
- Where does input 8B70F13430313231340300 in xml come from? Set a breakpoint on function module PROXY_CONVERT_TO_STRING,
and you can know this value is calculated based on the zip operation of the concatenated value based on all input parameter values ( in my example, it is CDI042416 )
- Where does qqqqqqqqqqqqqqqqqqqqqg== for IV_PRODUCT_GUID in xml come from?
Execute function module SSFC_BASE64_ENCODE in test mode, paste the fake guid AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA and execute it, then you get base64 encoded value. Paste the value into xml file.
- Now execute the test report once again. From callstack you can see that the MOCK_PROXY is called, the faked guid is fetched from XML and returned:
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2703838/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python中的單元測試框架:使用unittest進行有效測試Python框架
- 單元測試:單元測試中的mockMock
- 搞定Go單元測試(二)—— mock框架(gomock)GoMock框架
- Mock 在 Python 單元測試中的使用MockPython
- 使用JUnit進行單元測試
- 使用jest進行單元測試
- 使用Jest進行React單元測試React
- 如何使用MOQ進行單元測試
- 使用 Spring Boot 進行單元測試Spring Boot
- SAP CRM訂單模型CRMD_SHIPPING的單元測試方法模型
- Go 單元測試之mock介面測試GoMock
- 單元測試-mock使用應該注意什麼Mock
- PHP單元測試框架PHPUnit的使用PHP框架
- 使用SAP CRM External Interface進行訂單同步
- 使用SAP API portal進行SAP SuccessFactors的API測試API
- Golang 學習——基於 Gin 框架進行 httptest 單元測試Golang框架HTTP
- SAP CDS view單元測試框架Test Double介紹View框架
- 使用Angular CLI進行單元測試和E2E測試Angular
- go:極簡上手使用 stretchr/testify 進行mock測試GoMock
- Jest & enzyme 進行react單元測試React
- JavaScript單元測試框架JavaScript框架
- Google 單元測試框架Go框架
- 單元測試框架 mockito框架Mockito
- 換種思路寫Mock,讓單元測試更簡單Mock
- [譯]對 React 元件進行單元測試React元件
- 在C#中進行單元測試C#
- 掌握 xUnit 單元測試中的 Mock 與 Stub 實戰Mock
- 使用FakeAsync對Angular非同步程式碼進行單元測試Angular非同步
- 單元測試利器Mockito框架Mockito框架
- 前端單元測試框架梳理前端框架
- unittest 單元測試框架教程 1-執行測試指令碼框架指令碼
- JavaScript 測試教程-part 1:用 Jest 進行單元測試JavaScript
- 使用 PyHamcrest 執行健壯的單元測試REST
- 使用karma和jasmine配合phantom瀏覽器進行單元測試ASM瀏覽器
- 如何對Spring MVC中的Controller進行單元測試SpringMVCController
- linux環境程式設計(1): 實現一個單元測試框架Linux程式設計框架
- 單元測試模擬框架Mockito框架Mockito
- jest對react單元測試框架React框架