Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_

  • 2019 年 10 月 6 日
  • 笔记

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://jerry.blog.csdn.net/article/details/101197368

Approach one

Create a new class as wrapper for this API – create a new method whose name is exactly equal to API to be tested. In this method, just delegate the call to function module.

Then create a new unit test class via ABAP unit test framework. Disadvantage of this approach: the statement coverage report does not work as we expected. As long as our wrapper method is called, then statement coverage is always 100%, it will not drill down into function module.

The code marked as green color means they are executed. However, the tool will not display any further detail into this function module itself.

Approach two

As it is NOT supported to create local test class via wizard provided by ABAP unit test framework automatically, we have to manually create test class from scratch. Please see example in Q3R/111:

The drawback of this solution: as you can see, the local class is created based on function group level, not function module level, which means all function modules under this function group will be considered when unit test is performed, although there is not actually any test case for them. As a result, the final statement coverage would be extremely low since most of function modules are not tested at all.

In test coverage report for CRM_ORDERADM_I_PROD_DETERM_OW, the line with red color means the code is not executed during this unit test.

How to mock CRM_ORDERADM_H_READ_OB

This function module is used in determination API. It will first read from buffer, if fails, then read from DB.

How to achieve that it can successfully read our mocked data in unit test?

Solution

In setup method, just create a new dummy header structure for mock, with a new guid generated randomly. Then call PUT_OB to insert it to buffer, so that later the READ_OB would read it out.

How to mock CRM_LINK_GET_OW

This API will retrieve related partner information of current one order document via API CRM_LINK_GET_OW. However in my unit test, I am using a mocked order header structure. As a result I have to mock a fake partner as well.

Solution

Use the following code to insert a dummy link connecting one order header structure and partner:

How to mock COM_PARTNER_GET

Once Partner set guid is got, COM_PARTNER_GET will be called to read detail partner data. When debugging into this function module, I found that the read is first performed from buffer. As a result again we can insert mocked data to buffer.

Solution

insert mocked data to buffer via PUT_OB:

The complete source code could be found from AG3/001.

Action item for Jerry

Try to find if it is possible to only launch unit test for a given set of function modules instead of the whole function modules in the function group.