天天看點

Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution

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.

v

Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution

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.

Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution
The code marked as green color means they are executed. However, the tool will not display any further detail into this function module itself.
Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution

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:

Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution
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.
Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution
Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution
Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution
Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution
Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution
Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution
Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution
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.
Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution

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.

Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution
How to achieve that it can successfully read our mocked data in unit test?
Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution

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.

Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution

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.

Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_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.

Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution

insert mocked data to buffer via PUT_OB:

Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution

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.

Write Unit test for product Determination API CRM_ORDERADM_I_PROD_DETERM_Solution

繼續閱讀