ABAP Unit Test For Odata Services - SAP Blogs
ABAP Unit Test For Odata Services - SAP Blogs
Community
Nabheet Madan
March 20, 2018 5 minute read
So continuing the ABAP Unit Test momentum from previous blog, we have managed to create ABAP Unit
test for Odata services. The intent of this blog is to describe how to create local unit test classes for Odata
services.
Example Considered
In order to keep things simple we have created an Odata service which returns the description of the
document type (AUART from TVAKT table) eld based on the code passed to it. So I want to test my lter
functionality where we will Document type code and it shall return description. In case no description exists
then ABAP Unit test shall fail.
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 1/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
METHOD tvaktset_get_entityset.
DATA:lt_filters TYPE /iwbep/t_mgw_select_option,
ls_filter TYPE /iwbep/s_mgw_select_option,
ls_val TYPE /iwbep/s_cod_select_option.
Attributes
Variable 2 and 3 were added after we faced an exception in our general ow explained below.
Methods
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 2/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
ENDCLASS.
METHOD run_filter.
DATA: lt_filter_select_options TYPE /iwbep/t_mgw_select_option,
ls_filter_select_option TYPE /iwbep/s_mgw_select_option,
lt_filter TYPE /iwbep/t_cod_select_options,
ls_filter TYPE /iwbep/s_cod_select_option,
lr_data TYPE REF TO data,
ls_context TYPE /iwbep/if_mgw_appl_srv_runtime=>ty_s_mgw_response_context.
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 3/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
"read data
TRY .
cut->/iwbep/if_mgw_appl_srv_runtime~get_entityset(
EXPORTING
io_tech_request_context = mo_request_context_object
it_filter_select_options = lt_filter_select_options " Table of select options
IMPORTING
er_entityset = lr_data
es_response_context = ls_context
).
CATCH /iwbep/cx_mgw_busi_exception.
CATCH /iwbep/cx_mgw_tech_exception.
ENDTRY.
" If no data found for document type text then it is an exception
ASSIGN lr_data->* TO <tab>.
CALL METHOD cl_abap_unit_assert=>assert_not_initial
EXPORTING
act = <tab>. " Actual Data Object
ENDMETHOD.
ENDCLASS.
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 4/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
So that actually led us to something related to Request context parameter is missing. On googling found a
SAP Help which actually talked unit test integration and implementing a method to get the request context
We implemented this method before the service call and Bingo everything was working ne both for positive
as well as negative test cases. Revised RUN_FILTER method.
METHOD run_filter.
DATA: lt_filter_select_options TYPE /iwbep/t_mgw_select_option,
ls_filter_select_option TYPE /iwbep/s_mgw_select_option,
lt_filter TYPE /iwbep/t_cod_select_options,
ls_filter TYPE /iwbep/s_cod_select_option,
lr_data TYPE REF TO data,
ls_context TYPE /iwbep/if_mgw_appl_srv_runtime=>ty_s_mgw_response_context.
FIELD-SYMBOLS: <tab> TYPE table.
" Fill the data set and entity names to be unit tested
ms_request_context_struct-technical_request-source_entity_type = im_entity.
ms_request_context_struct-technical_request-target_entity_type = im_entity.
ms_request_context_struct-technical_request-source_entity_set = im_entityset.
ms_request_context_struct-technical_request-target_entity_set = im_entityset.
" Every DPC class now has INIT_DP_FOR_UNIT_TEST method which is used to provide a data
" instance which can be used for unit testing
mo_request_context_object = cut->/iwbep/if_mgw_conv_srv_runtime~init_dp_for_unit_test(
is_request_context = ms_request_context_struct
).
"read data
TRY .
cut->/iwbep/if_mgw_appl_srv_runtime~get_entityset(
EXPORTING
io_tech_request_context = mo_request_context_object
it_filter_select_options = lt_filter_select_options " Table of select options
IMPORTING
er_entityset = lr_data
es_response_context = ls_context
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 5/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
).
CATCH /iwbep/cx_mgw_busi_exception.
CATCH /iwbep/cx_mgw_tech_exception.
ENDTRY.
" If no data found for document type text then it is an exception
ASSIGN lr_data->* TO <tab>.
CALL METHOD cl_abap_unit_assert=>assert_not_initial
EXPORTING
act = <tab>. " Actual Data Object
ENDMETHOD.
Learning
In order to implement ABAP unit test, method INIT_DP_FOR_UNIT_TEST must be called to get the request
context.I hope this basic blog will help in implementing local test classes for all Odata services. Feel free to
provide your feedback.
Alert Moderator
Assigned tags
ABAP Development | ABAP Testing and Analysis | NW ABAP Gateway (OData) | OData | abap |
View more...
Related Questions
15 Comments
Michelle Crapo
Michelle
Sergio Fraga
Hello Nabheet,
Thanks for this blog and for the motivation to jump into TDD when developing OData services.
I would just like to add to everyone interesting in this topic that SAP provides a framework to automate tests
in our OData developements.
This framework is ECATT_ODATA, and it should help us de nining automated tests for the OData services. I
have tryed to explore a litlle but the tool, but I confess it’s not simple to use. With the tool we get a wizard to
create the tests with several options, but to my understaing the tool will generate the skeleton of the test
classes where we should go there and add the testing code.
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 7/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
The test clasees are global and they use the adition of FRIENDS to be able to access the *DPC_EXT and
MPC_EXT clases.
After some investigation I was not really convinved by the tool but nevertheless it’s good to know it exists.
If anyone has some good experience with this tool please share with us.
Again, great work Nabheet. and I prefer your solution for doing Unit Testing in OData development.
Thank you
Sérgio Fraga
Thanks for the feedback Sérgio Fraga.This is what i like about blogging by sharing my thoughts about unit
test script i came to know ECATT_Odata from you which am not aware. Let me try to understand the
framework and how it works. Lets keep learning and keep sharing our experience does not matter how
small they are.
Sergio Fraga
I am using your approach with one of my OData developments and now I can test my code without using
Gateway Client transaction!
Using Unit Tests give me the con dance that the code is working as expected and it’s much more fun to
work like this.
I would just like to refer that you have a bug in your code.
When you ll the PROPERTY of your LT_FILTER_SELECT_OPTIONS in your test code, your are passing the
value of the import parameter IM_AUART.
The test passes because in your productive code your are always reading the rst line of the lter, and in
your example it’s OK because you only have 1 lter, but if you had 2+ lters you should look at the lter
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 8/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
PROPERTY, which in your case should be ‘AUART’, since it’s the value you have de ned as the ABAP eld in
your SEGW project.
I would also suggest to create a repo in abapGit with your example so that anyone could pull the code and
test it right away. abapGit is an amazing project and we should make use of it.
Sérgio Fraga
Thanks Sergio for the feedback. Agreed was trying to demonstrate the MVP in quick time so missed it will
update.
One small request can you please all share your experience of TDD where you have used it will help us
Thanks
Nabheet
Sergio Fraga
Hello again,
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 9/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
My biggest advice is to read the book from Kent Beck, the father of TDD: Test Driven Development by
Example
Regarding OData Test Units, I am having a problem and I would like to ask you if you know how to solve it.
In your example, in the productive code you are using the table IT_FILTER_SELECT_OPTIONS. This table
comes with the properties lled with the names of the entity properties de ned in the SEGW project:
Normally, when we use lters in OData Development, we should use the following method to extract the
lters used in the request. This method will return a table with the ABAP Fields in the property and not with
the names:
The problem I am having is that if I use the method above to extract the lters from the request, when I am
running the Unit Test I will have a dump, because in the get_ lter( ) method there is a call to the
get_entity_type() method of the service model, and it seems that in Unit Test context, the service model is
not instantiated.
METHOD /iwbep/if_mgw_req_entityset~get_filter.
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 10/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
CATCH /iwbep/cx_mgw_med_exception .
EXIT.
ENDTRY.
ro_filter = mo_filter.
ENDMETHOD.
If you change your productive code to call the get_ lter_select_options() instead of using
IT_FILTER_SELECT_OPTIONS you will get the same issue.
Currently I am using the IT_FILTER_SELECT_OPTIONS and instead of using the ABAP eld names to extract
the correct lters from this table I am using the Entity Property names but I think we should always work
with the ABAP eld names since we are working in the backend service.
Sérgio Fraga
Alexander Chan
TYPE /IWBEP/CL_MGW_REQUEST_UNITTST=>TY_S_MGW_REQUEST_CONTEXT_UNIT
“`
” Every DPC class now has INIT_DP_FOR_UNIT_TEST method which is used to provide a data ” instance
which can be used for unit testing mo_request_context_object = cut-
>/iwbep/if_mgw_conv_srv_runtime~init_dp_for_unit_test( is_request_context =
ms_request_context_struct ).
“`
Former Member
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 11/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
Hi,
does this also work for oData-Services that are generated based on corresponding CDS Views? The mid and
long term way of implementing an oData service is potentially no longer manually coding via SEGW but
modelling a corrsponding CDS View with the oData Annotation.
BR,
Holger
Dear Holger
We have two options here as per my understanding as i dont see any annotation provided in CDS which
actually make sense.
1. Option one is to use ECATT_Odata as mentioned in previous comments which will generate the
framework and classes based on your CDS
2. Manual one.
Thanks
Nabheet
Sergio Fraga
Well,
From the open SAP course “Writing Testable Code for ABAP” we discovered that SAP is delivering a ABAP
CDS Test Double Framework
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 12/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
Alexander Chan
Thanks for pointing to that course. Nice to see info about all the test doubles. It’s one way that the
io_tech_request_context might be able to be mocked.
Former Member
Hi Nabheet,
Can you atach a test example for “get_entity” implementation method? I don’t know how to set the key.
Thank you!!
Tobias Trapp
Hi Nabheet,
great work I have another question: Do you see a chance to test the MPC_EXT class as well? The use
case is to to test a rede nition of the de ne method.
Best Regards,
Tobias
Koen Boutsen
Hello.
Your odata method has an exporting parameter er_entity_set = lr_data. The type of this element is ‘data’.
Do you know how you can evaluate the content of the elements and properties of these elements ?
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 13/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
I mean something like assertEquals( exp = ‘Man’ act = lr_data-gender ). Is there any way of casting or
converting that can be used ?
Best Regards
Koen
Andreas Kopp
Hello Nabheet.
Thank you for this example. I have a similar case where I want to pass lter parameters to the get_entityset
method. The only di erence is that I want to use the get_ lter method of io_tech_request_context instead of
it_ lter_select_options.
In the get_entityset method I use the following code to get the lters from the request:
DATA(lo_filter) = io_tech_request_context->get_filter( ).
DATA(lt_filter_select_options) = lo_filter->get_filter_select_options( ).
" Fill the data set and entity names to be unit tested
me->ms_request_context_struct-technical_request-source_entity_type = 'MyEntity'.
me->ms_request_context_struct-technical_request-target_entity_type = 'MyEntity'.
me->ms_request_context_struct-technical_request-source_entity_set = 'MyEntitySet'.
me->ms_request_context_struct-technical_request-target_entity_set = 'MyEntitySet'.
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 14/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
me->ms_request_context_struct-technical_request-filter_object =
NEW /iwbep/cl_mgw_req_filter(
iv_filter_string = |( ( SapObjectType eq 'BUS2001' ) and ( SapObjectId eq 'PR-1000' ) )|
it_filter_select_options = lt_filter_select_options ).
" Every DPC class now has INIT_DP_FOR_UNIT_TEST method which is used to provide a data
" instance which can be used for unit testing
mo_request_context_object = cut->/iwbep/if_mgw_conv_srv_runtime~init_dp_for_unit_test(
is_request_context = me->ms_request_context_struct
).
When I run the unit test, I get the following output (the messages are partly German):
'Im ST-Program /IWBEP/ST_ANY_DATA ist bei der Serialisierung ein Fehler aufgetreten.'
('There was an error with the serialization in the ST-program /IWBEP/ST_ANY_DATA')
Stack:
How can I work in unit tests with the technical request context and lters?
Kind regards
Andreas
Add Comment
Find us on
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 15/16
10/2/2020 ABAP Unit Test for Odata Services | SAP Blogs
Newsletter Support
https://blogs.sap.com/2018/03/20/abap-unit-test-for-odata-services/# 16/16