During development of our Cloud Foundry Java app we had the requirement to call an OData service in SAP S/4HANA. We had to call this in a generic way and did not know during development which OData will be called during runtime.
We decided to use the SAP Cloud SDK to get support for these OData calls.
Now after everything was implemented, we wondered how to setup integration tests for such calls.
In this blog post I will explain how we decided to test such calls. Other teams might come up with other solutions. Perhaps we can discuss other options here.
If working with the SAP Cloud SDK for calling an OData in a SAP S/4HANA system two things need to be mocked:
Mock for Destination Service
In our project we use the destination service to retrieve the sap-client from the destination the customer must define. In the tests we also want to retrieve the sap-client from the destination to check if the client is correctly used in the OData call.
To mock the destination service, the SAP Cloud SDK provides a mock util. To use it the following dependency needs to be added in the pom:
In the BeforeClass method we configure and start the web server. In the AfterClass method we stop the web server.
Before each test we reset the server and create a stub for the mock server. This could also be done in each test by creating different stubs for each test.
In the stub we check the URL and two header parameters and if the request matches these conditions the defined response is returned. So, in tests we can now check if the response we expect from the return of the stub match.
In this example we would call the backend system in our productive coding with the destination UNIT_TEST_DESTINATION. In the settings in systems.yaml we have defined that the URL for this destination is http://localhost:1111/.
Because we call our productive coding with the relative path /odata/Entity?$format=json the complete URL which is called is: