Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
Showing results for 
Search instead for 
Did you mean: 
Former Member

The SAPUI5/OpenUI5 MockServer component is a simple yet powerful tool to simulate the backend connectivity to a server. Any server.

We already know that the MockServer can simulate OData services. This is specifically useful during development and testing of OData-based SAP Fiori and SAPUI5 apps.

However, it is also possible to simulate non-OData RESTfull services with the MockServer. Since the MockServer simulates HTTP connectivity it can be used for simulation of any REST API.

In order to do that all you have to do is to instantiate the MockServer with your own custom mock requests instead of using the simulation API (that is OData specific).

For example:

Open Weather API

The 3rd party API that we will mock is the Open Weather API.  The specific URL that we will use in this post is described here: forecast5.

Create a (mocked) request handler

The documentation for SAPUI5 MockServer setRequests gives a good description of the request structure:

method: “GET”|”POST”|”DELETE|”PUT”,

path: “/path/to/resource”,

response: function(xhr, param1, param2, …) { }

We begin to define our request according to this structure:

var oByCityName = {
      method: “GET”,
      path: new RegExp(“forecast\?q=(.*)”),
      response: function(oXhr, sUrlParams) {
  "Incoming request for Call 5 day / 3 hour forecast data");
            var sCityNameAndCode = sUrlParams;
            switch (sCityNameAndCode) {
                  case “Shuzenji,jp”:
                              oXhr.respondJSON(200, {}, JSON.stringify({
                                         "weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
                                         "dt_txt":"2014-07-23 09:00:00"}
                                 ] }));
                    oXhr.respondJSON(200, {}, JSON.stringify({});
               return true;

Set up the MockServer instance

var oMockServer = new MockServer({
rootUri: “/”,
requests: [oByCityName, …]
//start the mockserver

That's it :smile: