cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a test framework to mock ABAP OData Client Proxy for Unit Testing?

jhodel18
Active Contributor

So I created a Model Class that provides data from a remote OData service.

Here's the source code: zcl_product_model_es

This class is making use of the OData Client Proxy for OData V2 (class interface `/iwbep/if_cp_client_proxy`).

Is there a test framework that intercepts this client proxy and allows the capability for mocking its response for use in unit testing? I tried to check the official documentation, but can't find any.

I'm looking for something similar to RAP test doubles interfaces like if_cds_test_environment and if_osql_test_environment. See Basic Test Class Structure.

Thanks and regards,

Jhodel

Accepted Solutions (1)

Accepted Solutions (1)

matt
Active Contributor

You can always wrap any class in another class implemented wrt to an interface, thereby allowing easy mocking.

Create an interface with methods of the same name as those you want to us in the proxy. Create a wrapper class that implements those methods, and calls the proxy methods. In your productive code, instead of calling the proxy methods directly, you instantiate the wrapper class as a reference to the interface, and and call its method.

In your unit test code, you instantiate instead a mock implementation of the interface.

jhodel18
Active Contributor

Hi matthew.billingham,

Thanks for your answer! When you say it like that then I guess that's a valid way, but I was probably missing some context in my original question. From the documentation of the RAP framework, SAP has provided test frameworks to easily create test doubles -- see Basic Test Class Structure. Referring to if_cds_test_environment and if_osql_test_environment.

With your answer above, I take it that SAP didn't provide any for the client proxy so it's up to us to create the class wrapper, am I right?

PRIVATE SECTION.
CLASS-DATA: class_under_test     TYPE REF TO lhc_name,
            cds_test_environment TYPE REF TO if_cds_test_environment,
            sql_test_environment TYPE REF TO if_osql_test_environment,
            go_client_proxy      TYPE REF TO /iwbep/if_cp_client_proxy.
matt
Active Contributor

There is the ABAP Test Double Framework. If it works with your proxy, I don't know - probably though. I never use it myself - I prefer to do it myself, as think it's easier to maintain, and less prone to error.

jhodel18
Active Contributor
0 Kudos

That sounds interesting! I will give it a try -- thanks a lot!

FredericGirod
Active Contributor

Otherwise, you could add a Setter method (or constructor).

First you need to change your getter method to check if Proxy_Client is already bounded.(and save it in a global variable)

And in a new method you just force the value of the proxy client.

So when you are testing the class, you create an instance of a mock proxy, and when you will test a method and it will request the Proxy_client, it will used the mocked proxy.

jhodel18
Active Contributor
0 Kudos

You just described the dependency injection pattern. I did exactly that. I have the code here: test class.

Using ABAP Test Double Framework did work! Setting it up is quite tedious due to the nature of the nested object for the client proxy but still, it did the job! I can see the benefit of using the framework since I don't have to create an implementation class myself for each interface used.

Thanks Matthew! I learned from you the existence of TDF!

Answers (0)