Show TOC

Mock Server: Frequently Asked QuestionsLocate this document in the navigation structure

Is the mock server a real server?

No. The mock server runs on the client and only uses the server terminology of 'start' and 'stop'. It does not require a network connection since there is no actual server involved.

What require() is needed?
The mock server needs the following require statement:
jQuery.sap.require("sap.ui.core.util.MockServer"); 
Can we use one mock server instance to mock multiple OData services?

No. Each OData service needs its own mock server. Create one MockServer instance per service.

How to obtain metadata xml?

Call the metadata of the service in a browser and save it into a file.

How to obtain mock data? What options do I have for mock data?
You can let the mock server generate random mock data automatically based on services metadata. For this, provide only the path to the metadata file and omit the second parameter of the simulate function as follows:
// url to the service metadata document 
        var sMetadataUrl = "testdata/rmtsampleflight/metadata.xml"; 
        oMockServer.simulate(sMetadataUrl);
You can provide your own mock data in .json files, which can either be created manually or saved from an OData service response. Mock data in JSON format can be generated from an OData service by adding the $format=json parameter to the URL. Save the browser response which is called <entity set name>.json, for example FlightCollection.json and put it into the model folder. Add the path to the simulate function:
// url to the service metadata document 
        var sMetadataUrl = "testdata/rmtsampleflight/metadata.xml";
        // base url which contains the mockdata
        var sMockdataBaseUrl = "testdata/rmtsampleflight/";
        oMockServer.simulate(sMetadataUrl, sMockdataBaseUrl);
You can specify a path to .json mock data and let the mock server generate data for the rest of the service entities:
var sMetadataUrl = "testdata/rmtsampleflight/metadata.xml"// url to the service metadata document
        var sMockdataBaseUrl = "testdata/rmtsampleflight/"// base url which contains the mockdata
            oMockServer.simulate(sMetadataUrl, {
                'sMockdataBaseUrl' : sMockdataBaseUrl,
                'bGenerateMissingMockData' : true
            });
You can specify the names of the entity sets that are needed, and the mock server will load data only for the specified service entities:
var sMetadataUrl = "testdata/rmtsampleflight/metadata.xml"// url to the service metadata document
        var sMockdataBaseUrl = "testdata/rmtsampleflight/"// base url which contains the mockdata
            oMockServer.simulate(sMetadataUrl, {
                'sMockdataBaseUrl' : sMockdataBaseUrl,
                'bGenerateMissingMockData' : true,
                'aEntitySetsNames' : ["EntitySetName1", " EntitySetName2"]
            });
I'm using the OData model and I get the following error in the console: The following problem occurred: no handler for data
The OData model uses JSON to fetch the data:
var oModel = new sap.ui.model.odata.ODataModel(sUri, true);
What do I put in the rootUri?

Verify that you use the exact same URI prefix in the request as in the rootUri you define for the mock server. If a root URI is set, all request path URIs are prefixed with this root URI. The root URI has to be relative and requires a trailing '/'. It also needs to match the URI set in OData/JSON models or simple XHR calls in order for the mock server to intercept them.

The code snippet shows an example:
var sUri = "/mock/";
var oMockServer = new sap.ui.core.util.MockServer({
    rootUri : sUri
});
var oModel = new sap.ui.model.odata.ODataModel(sUri, true);
Can the mock server be used for more than for OData service simulation?

Yes. The mock server can be used to help you fake server response on any given API and stub all AJAX access to resources such as OData service, metadata, annotation files (XML), other JSON or *.properties files.

Is OData navigation supported?

The mock server supports navigation via association also if no referential constraint is defined. However,the result of the navigation is the entire collection of the navigation, or the first entry of the collection according to the association multiplicity. So, if you wantor needs the navigation to return "correct" results according to keys, define a respective referential constraint.

Note

Due to a limitation of the mock server, you can not use the same association to describe a two-way navigation. If the navigation shall work for both directions, you need to define an appropriate association for each direction.

How can I use the mock server in a QUnit?
You can set up the mock server in the setup function; example:
var _oMockServer = undefined;
        module("OData data provider", {
            setup : function() {
                jQuery.sap.require("sap.ui.app.MockServer");
                this._oMockServer = new sap.ui.app.MockServer({ rootUri: "/model/"});
                this._oMockServer.simulate("../../../../qunit/service/metadata.xml");
                this._oMockServer.start();
            },
            teardown : function() {
                this._oMockServer.stop();
            }
        });
How can I provide exit functions as pre/post functions of requests?

Mock Server has APIs to provide more flexibility and control over its current request processing. During request processing, the callbacks are called before or after native handling of the Mock Server using the SAPUI5 eventing mechanism. You can add a callback in all requests of a specific HTTP method, for example in all get requests, but additionally also on a specific entity set name, for example, POST to SaleOrders).

// add a callback in all requests of a specific http method
oMockServer.attachAfter(sap.ui.core.util.MockServer.HTTPMETHOD.GET, fnCbPost);
// on a specific entityset name
oMockServer.attachAfter(sap.ui.core.util.MockServer.HTTPMETHOD.GET, fnCbPost, "CarrierCollection");
// remove the callback
oMockServer.detachAfter(sap.ui.core.util.MockServer.HTTPMETHOD.GET, fnCbPost);
If you add additional request handlers and want to use this hooks mechanism inside your response function, just call:
this.fireEvent(sap.ui.core.util.MockServer.HTTPMETHOD.GET + 'sEntityset' + ':before' , {oXhr: oXhr, sUrlParameters: sUrlParameters});
What do I need to do to run an OPA test with mock server

Start you app in mock mode. It is not possible to declare a mock server outside the app's context.