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) {
            jQuery.sap.log.debug("Incoming request for Call 5 day / 3 hour forecast data");
            var sCityNameAndCode = sUrlParams;
            switch (sCityNameAndCode) {
                  case “Shuzenji,jp”:
                              oXhr.respondJSON(200, {}, JSON.stringify({
                              "city":{"id":1851632,"name":"Shuzenji",
                              "coord":{"lon":138.933334,"lat":34.966671},
                              "country":"JP",
                              "cod":"200",
                              "message":0.0045,
                              "cnt":38,
                              "list":[{
                                           "dt":1406106000,
                                           "main":{
                                          "temp":298.77,
                                          "temp_min":298.77,
                                          "temp_max":298.774,
                                          "pressure":1005.93,
                                          "sea_level":1018.18,
                                          "grnd_level":1005.93,
                                          "humidity":87
                                          "temp_kf":0.26},
                                         "weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
                                         "clouds":{"all":88},
                                         "wind":{"speed":5.71,"deg":229.501},
                                         "sys":{"pod":"d"},
                                         "dt_txt":"2014-07-23 09:00:00"}
                                 ] }));
               break;
               default:
                    oXhr.respondJSON(200, {}, JSON.stringify({});
               }
               return true;
          }
      }
};


Set up the MockServer instance

var oMockServer = new MockServer({
rootUri: “/api.openweathermap.org/data/2.5/”,
requests: [oByCityName, …]
});
//start the mockserver
oMockServer.start();

That’s it 🙂

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply