List of FAQs
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.
The mock server needs the following require statement:
jQuery.sap.require("sap.ui.core.util.MockServer");
No. Each OData service needs its own mock server. Create one MockServer instance per service.
Call the metadata of the service in a browser and save it into a file.
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 type name>.json, for example Flight.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 });
The OData model uses JSON to fetch the data:
var oModel = new sap.ui.model.odata.ODataModel(sUri, true);
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);
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.
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.
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.
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(); } });