How to create unit test for product determination function module

简介: How to create unit test for product determination function module

For more detail about how product determination works in one order scenario, please refer to this blog: Product Alternative ID used in Opportunity Line item product determination

As it is not possible to only create the test class for a given function module via ABAP unit test framework, but the test class has to be created on function group level:


image.png

For demonstration purpose I would not like to modify SAP standard function group, so I create a new local class as wrapper with a method name which is exactly equal to the function module name. As a result now I can just create local test class against this wrapper class by ABAP unit test wizard:

image.png

As displayed above, I have two test methods DETERMINE_OK and DETERMINE_FAIL to test these two boundary conditions.

For determination failure case, it is easy to implement, just pass an non-existing ordered product as input parameter value for iv_ordered_product. If determination fails as expected, es_product_detail and cv_ordered_prod will remain initial.


image.png

For successful determination case, I expect that the standard product id ( gv_prod_id ) should be determined based on alternative ID ( gv_altid ).


image.png

In my unit test, the alternative ID value contained in gv_altid and the product id contained in gv_prod_id are fake, neither of them point to a real product in the system. When I debug the product determination based on real product performed in WebUI, I found these two database tables are joined for determination.



image.png

As a result, I plan to create corresponding dummy entry in these two database tables in the SETUP method of test class:

CALL FUNCTION 'GUID_CREATE'

     IMPORTING

       ev_guid_16 = mv_fake_prod_guid.

   ls_mock_alt_id = VALUE #( product_guid = mv_fake_prod_guid

          upname = sy-uname

          altvehno = gv_altid ).

   INSERT isam_o_veh_ids FROM ls_mock_alt_id.

   DATA(prod) = VALUE comm_product( product_guid = mv_fake_prod_guid

        product_id = gv_prod_id

        product_type = '01' upname = sy-uname ).

   INSERT comm_product FROM prod.

   COMMIT WORK AND WAIT.

And remove them in TEARDOWN:

 METHOD teardown.

   DELETE FROM isam_o_veh_ids WHERE product_guid = mv_fake_prod_guid.

   DELETE FROM comm_product WHERE product_guid = mv_fake_prod_guid.

   COMMIT WORK AND WAIT.

 ENDMETHOD.

In the meantime, the determination function module needs a guid for one order header object ORDERADM_H. When the function module is executed, the header data will be read from buffer. In my unit test, in order to ensure that the header read from buffer succeed, I have to first insert the buffer of dummy object header via the following code in SETUP method:

DATA: ls_mock_header TYPE crmt_orderadm_h_wrk,

         ls_mock_alt_id TYPE isam_o_veh_ids,

         lt_link        TYPE crmt_link_comt,

         ls_link        LIKE LINE OF lt_link.

   CREATE OBJECT f_cut.

   CALL FUNCTION 'GUID_CREATE'

     IMPORTING

       ev_guid_16 = mv_header.

   ls_mock_header-guid = mv_header.

   ls_mock_header-process_type = gv_oppt_proc_type.

   CALL FUNCTION 'CRM_ORDERADM_H_PUT_OB'

     EXPORTING

       is_orderadm_h_wrk = ls_mock_header.

The complete source code could be found from here: https://github.com/i042416/jerryslide/blob/master/ABAP/132_Product_Determine_AUnit1.abap

The unit test could successfully be executed and all tests pass:


image.png

However, when looking back about the unit test implementation, I find the test has dependency on the transaction type which must exist in current system and has been configured with Alternative ID correctly.

Suppose my unit test code is transported to a testing system where type “ZJER” does not exist, the unit test will fail there.

] image.png

As a result, the improvement would be:


(1) Change the constant value of process type from “ZJER” to a type which does not exist in any system, for example “ABCD”:

image.png

(2) Append the following source code to the end part of SETUP method, to create a temporary transaction type for unit test purpose:

image.png

(3) Remove the temporary transaction type in TEARDOWN method:

image.png


相关文章
|
2月前
|
数据库
如何通过增强(Enhancement)的方式,给 ABAP Function Module 增添新的功能试读版
如何通过增强(Enhancement)的方式,给 ABAP Function Module 增添新的功能试读版
26 0
|
6月前
|
数据库
关于 SAP ABAP Enqueue Function Module 的输入参数 _wait
关于 SAP ABAP Enqueue Function Module 的输入参数 _wait
32 0
|
6月前
|
BI
SAP ABAP 系统里和传输请求读写相关的 Function Module
SAP ABAP 系统里和传输请求读写相关的 Function Module
44 0
|
1月前
|
BI
工具分享 - 将一个 ABAP Function Group 内所有 Function Module 按照代码行数从高到低排序并显示试读版
工具分享 - 将一个 ABAP Function Group 内所有 Function Module 按照代码行数从高到低排序并显示试读版
15 0
|
3月前
|
缓存
pytest 运行测试函数报错的解决办法 TypeError: calling <function xxx> returned None, not a test
pytest 运行测试函数报错的解决办法 TypeError: calling <function xxx> returned None, not a test
97 0
|
3月前
|
数据库
SAP ABAP 更新函数(Update Function Module)执行出错的原因分析试读版
SAP ABAP 更新函数(Update Function Module)执行出错的原因分析试读版
35 0
|
3月前
|
数据库
130. SAP ABAP 更新函数(Update Function Module)执行出错的原因分析
130. SAP ABAP 更新函数(Update Function Module)执行出错的原因分析
25 0
|
3月前
答网友疑问:ABAP Function Module 如何支持内表结构不确定的动态输入参数试读版
答网友疑问:ABAP Function Module 如何支持内表结构不确定的动态输入参数试读版
12 0
|
3月前
127. 答网友疑问:ABAP Function Module 如何支持内表结构不确定的动态输入参数
127. 答网友疑问:ABAP Function Module 如何支持内表结构不确定的动态输入参数
19 0
|
6月前
|
开发者
如何使用 ABAP Function Module SEO_CLASS_CREATE_COMPLETE 创建 ABAP class
如何使用 ABAP Function Module SEO_CLASS_CREATE_COMPLETE 创建 ABAP class
39 0

热门文章

最新文章