Skip to Content
avatar image
Former Member

OpenUI5 Mockserver configuration

Hi,

Can anyone please explain how to config mock server in SAPUI5 and how to set rootUri.

What exactly rootUri pointing to?

Thanks.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • avatar image
    Former Member
    Jul 21, 2017 at 06:02 AM

    The rootUri is an artificial URI you can choose how you like. The important thing is that you point your oData model to that URI once the mockserver is running.

    I always use the URI of the real service on the gateway. That way I can use the same model in my code.

    Let me give you an example:

    // assuming that your real service has the URI /sap/opu/odata/sap/my_service
    // DO NOT forget the trailing slash!
    sServiceUri = "/sap/opu/odata/sap/my_service/"
    
    // in your component.js you have to detect that you are running local (or any other condition for starting the mockserver)
    if(location.hostname === "localhost" || location.hostname === "127.0.0.1"){
       mockserver.init(sServiceUri);
    }
    
    // in your mockserver.js you have to initialize the mockserver
    this.oMockServer = new MockServer({
                    rootUri: sServiceUri
    }); 
    MockServer.config({
       autoRespond: true,
       autoRespondAfter: 100
    });
    // get path to your metadata.xml and mockdata folder
    var sPath = jQuery.sap.getModulePath("com.your.app.id.localService");
    
    this.oMockServer.simulate(sPath + "/metadata.xml", sPath + "/mockdata/");
    this.oMockServer.start();
    
    // now you can create your model the same way you did it with a real service, e.g.
    this._oModel = new sap.ui.model.odata.v2.ODataModel(sServiceUri);
    
    Add comment
    10|10000 characters needed characters exceeded