Data provider classes are enabled for unit tests. A data provider (specialization of /IWBEP/CL_MGW_ABS_DATA) requires the request context (a realization of the /IWBEP/IF_MGW_REQ_X interfaces) to be initialized in order to receive the information provided through the consumer request.
An implementation of the request context is available with /IWBEP/CL_MGW_REQUEST_UNITTST which is a subclass of /IWBEP/CL_MGW_REQUEST. The new request context is exposed in the package interface /IWBEP/MGW_GSR_CORE_CNT, hence is accessible for the data provider classes (DPCs). In addition, the new method INIT_DP_FOR_UNIT_TEST has been added to the interface /IWBEP/IF_MGW_CONV_SRV_RUNTIME. This method is used to initialize a data provider instance for the usage in a unit test.
The process is as follows:
An instance of the new request context is created.
The request values are assigned to the request context instance.
Member variables of the /IWBEP/CL_MGW_ABS_DATA are initialized and set:
MO_CONTEXT
MR_REQUEST_DETAILS
MR_SERVICE_DOCUMENT_NAME
MR_SERVICE_VERSION
MR_SERVICE_NAMESPACE
Logger and message container instances are retrieved.
Caution
The implementation of the request context can only be used within the unit test, not during usual processing through the SAP NetWeaver Gateway runtime.
Create the unit test class of your DPC.
Declare the following variables in the definition section of your unit test class:
The request context object:
data: mo_request_context_object type ref to /iwbep/cl_mgw_request_unittst.
The request context structure to be filled in the unit test class:
data: ms_request_context_struct type /iwbep/cl_mgw_request_unittst=>ty_s_mgw_request_context_unit.
The data provider instance of your data provider:
data: mo_data_provider type ref to /iwbep/cl_tea_data_provider.
Create the data provider instance in the setup method of your unit test:
method setup.
* instantiate the Data Provider
create object mo_data_provider .
Test your DPC methods by using test_get_entityset_managers, for example.
Fill the request context structure with the values corresponding to your use case, for example navigation path, order, select, keys, entity set, etc.
ms_request_context_struct-technical_request-source_entity_type = 'Manager'.
ms_request_context_struct-technical_request-target_entity_type = 'Manager'.
ms_request_context_struct-technical_request-source_entity_set = 'Managers'.
ms_request_context_struct-technical_request-target_entity_set = 'Managers'.
In order to simulate converted keys, action parameters or filters later on, they need to be set in the request context structure explicitly:
DATA: lo_filter TYPE REF TO /iwbep/if_mgw_req_filter.
DATA: ls_converted_keys TYPE REF TO data.
DATA: ls_converted_parameters TYPE REF TO data.
ms_request_context_struct-technical_request-converted_keys = ls_converted_keys.
ms_request_context_struct-technical_request-conv_action_parameters = ls_converted_parameters.
ms_request_context_struct-technical_request-filter_object = lo_filter .
Call the method below in order to initialize the data provider instance for the unit test:
mo_request_context_object = mo_data_provider->/iwbep/if_mgw_conv_srv_runtime~init_dp_for_unit_test ( is_request_context = ms_request_context_struct ).
Now you can call the data provider implementation as usual. For your convenience the /iwbep/if_mgw_appl_srv_runtime methods have been simplified to only import the technical request context object.
Syntax
try . so_data_provider->/iwbep/if_mgw_appl_srv_runtime~get_entityset( exporting io_tech_request_context = so_request_context_object importing er_entityset = lr_data es_response_context = ls_response_context ). catch /iwbep/cx_mgw_busi_exception. catch /iwbep/cx_mgw_tech_exception. endtry.
Implement the corresponding unit test assertions and checks as required.
cl_aunit_assert=>assert_not_initial( act = lr_data ).
You can find an example unit test created for the data provider class /IWBEP/CL_TEA_DATA_PROVIDER in the system.