How to Generate Mock Data in UI5

Introduction

As a UI5 developer or a UI5 tester, you may often need to generate mock data for testing your apps. This can be a time-taking task, especially when you need to test a large amount of data. Fortunately, we have several tools provided by SAP to make this process faster and more efficient. In this article, we will explore the best practices for generating mock data in UI5.

What is a Mock Data?

Mock data, also known as fake data or dummy data, is data that is created for the purpose of testing or demonstrating a software application without using real production data. Mock data is often used in software development to simulate different scenarios or test the functionality of an application under different conditions. Mock data can be used to create test cases, generate sample data for demos, or simulate user behavior for usability testing. The data used in mock data can be randomly generated or created manually to mimic the format and structure of real data. Using mock data helps ensure that software applications are tested thoroughly before they are released to end-users and that they function correctly under a wide range of conditions.

When do we need Mock Data in SAP UI5?

Mock data is commonly used in SAP UI5 development for several reasons, including:

  1. Testing: Mock data is used to test the application’s functionality under different conditions without the need for real data. It helps to identify and fix any issues before the application is deployed in a production environment.
  2. Prototyping: Mock data is useful for quickly prototyping and demonstrating the application’s features and functionality without having to connect to real data sources.
  3. Performance: Using real data during the development process can slow down the application’s performance. Mock data helps developers test and optimize the application’s performance before deploying it in a production environment.
  4. Security: Mock data can be used to ensure that sensitive or confidential data is not exposed during development and testing.

Overall, using mock data in SAP UI5 development helps to reduce development time and costs while ensuring that the application meets the requirements and functions as expected.

Steps to Generate Mock Data in SAP UI5

There are several methods for generating mock data in SAP UI5, depending on your specific needs and preferences. Here are some general steps that you can follow to generate mock data in SAP UI5:

  1. Identify the data requirements: Determine the type of data and the format needed for your application. This may include data types, fields, and data structure.
  2. Choose a method: Select the method that best suits your needs for generating mock data. You can use online tools, create custom scripts, or leverage existing data sources.
  3. Generate the mock data: Use the chosen method to generate the mock data. If using an online tool, enter the data requirements and follow the prompts to generate the data. If creating custom scripts, use JavaScript or other scripting languages to create the data.
  4. Save the mock data: Save the generated mock data in a JSON format or any other format that is compatible with SAP UI5.
  5. Integrate the mock data: Use the mock data in your SAP UI5 application by either manually including it in your code or loading it dynamically from a JSON file.
  6. Test the application: Test the application with the mock data to ensure that it functions correctly under different conditions.

Overall, generating mock data in SAP UI5 requires careful planning, selecting the right method, and thorough testing to ensure that the application functions as expected.

Generate Custom Mock Data in SAP UI5

How to Generate Mock Data in UI5 using WebIDE

Following are the steps to generate Mock Data in SAP UI5 using WebIDE:

Step 01: Create a new UI5 project (Neo or Cloud Foundry) in SAP WebIDE

Step 02: Download the metadata (e.g. of Northwind from here: https://services.odata.org/v2/northwind/northwind.svc/$metadata)

Step 03: Right-click on the project folder and click Import OData, and use the file downloaded above to import the same.

Step 04: Once imported, do not change anything in Manfiest.xml, ui5.yaml file or servicebinding.js file. You will see a new file “metadata.xml” within the folder “localService” Right click on the file and click “Edit Mock Data”.

Edit Mock Data

Step 05: There you need to choose your entityset (in our usecase Products) and click the button “Generate Random Data”

generate random data

Step 06: In your view file, create a table and bind one of the entityset (in our usecase Products) to items, and the fields of the same within the table fields.

Step 07: Run your web app using a new configuration, and mark “Run using mockdata” checkbox.

That’s it, your mockdata would be binded with your table.

Output of Mock Data

Code

Manifest File:

{
    "_version": "1.12.0",
    "sap.app": {
        "id": "MockData.MockData",
        "type": "application",
        "i18n": "i18n/i18n.properties",
        "applicationVersion": {
            "version": "1.0.0"
        },
        "title": "{{appTitle}}",
        "description": "{{appDescription}}",
        "sourceTemplate": {
            "id": "servicecatalog.connectivityComponentForManifest",
            "version": "0.0.0"
        },
        "dataSources": {
            "NorthwindModel": {
                "uri": "/here/goes/your/serviceurl/",
                "type": "OData",
                "settings": {
                    "localUri": "localService/metadata.xml"
                }
            }
        }
    },
    "sap.ui": {
        "technology": "UI5",
        "icons": {
            "icon": "",
            "favIcon": "",
            "phone": "",
            "phone@2": "",
            "tablet": "",
            "tablet@2": ""
        },
        "deviceTypes": {
            "desktop": true,
            "tablet": true,
            "phone": true
        }
    },
    "sap.ui5": {
        "flexEnabled": false,
        "rootView": {
            "viewName": "MockData.MockData.view.View1",
            "type": "XML",
            "async": true,
            "id": "View1"
        },
        "dependencies": {
            "minUI5Version": "1.65.6",
            "libs": {
                "sap.ui.layout": {},
                "sap.ui.core": {},
                "sap.m": {}
            }
        },
        "contentDensities": {
            "compact": true,
            "cozy": true
        },
        "models": {
            "i18n": {
                "type": "sap.ui.model.resource.ResourceModel",
                "settings": {
                    "bundleName": "MockData.MockData.i18n.i18n"
                }
            },
            "": {
                "type": "sap.ui.model.odata.v2.ODataModel",
                "settings": {
                    "defaultOperationMode": "Server",
                    "defaultBindingMode": "OneWay",
                    "defaultCountMode": "Request"
                },
                "dataSource": "NorthwindModel",
                "preload": true
            }
        },
        "resources": {
            "css": [
                {
                    "uri": "css/style.css"
                }
            ]
        },
        "routing": {
            "config": {
                "routerClass": "sap.m.routing.Router",
                "viewType": "XML",
                "async": true,
                "viewPath": "MockData.MockData.view",
                "controlAggregation": "pages",
                "controlId": "app",
                "clearControlAggregation": false
            },
            "routes": [
                {
                    "name": "RouteView1",
                    "pattern": "RouteView1",
                    "target": [
                        "TargetView1"
                    ]
                }
            ],
            "targets": {
                "TargetView1": {
                    "viewType": "XML",
                    "transition": "slide",
                    "clearControlAggregation": false,
                    "viewId": "View1",
                    "viewName": "View1"
                }
            }
        }
    }
}

 

Service Binding File:

function initModel() {
    var sUrl = "/here/goes/your/serviceurl/";
    var oModel = new sap.ui.model.odata.ODataModel(sUrl, true);
    sap.ui.getCore().setModel(oModel);
}

 

View File:

<mvc:View controllerName="MockData.MockData.controller.View1" xmlns:mvc="sap.ui.core.mvc" displayBlock="true" xmlns="sap.m">
    <Shell id="shell">
        <App id="app">
            <pages>
                <Page id="page" title="Generate Custom Mock Data in SAP UI5">
                    <content>
                        <Table items="{/Products}">
                            <columns>
                                <Column width="12em">
                                    <Text text="Product"/>
                                </Column>
                                <Column minScreenWidth="Tablet" demandPopin="true">
                                    <Text text="Supplier"/>
                                </Column>
                            </columns>
                            <items>
                                <ColumnListItem vAlign="Middle">
                                    <cells>
                                        <ObjectIdentifier title="{ProductName}" text="{ProductId}"/>
                                        <Text text="{SupplierID}"/>
                                    </cells>
                                </ColumnListItem>
                            </items>
                        </Table>
                    </content>
                </Page>
            </pages>
        </App>
    </Shell>
</mvc:View>

 

How to Generate Mock Data in UI5 using BAS

The content will be added soon.

Author

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.