---
title: "Creating Mock Services for Test Automation in Business Central"
id: "997964"
type: "post"
slug: "creating-mock-services-for-test-automation-in-business-central"
published_at: "2024-02-14T09:50:57+00:00"
modified_at: "2026-09-02T06:07:28+00:00"
url: "https://ioi.gmbh/en/2024/02/14/creating-mock-services-for-test-automation-in-business-central/"
markdown_url: "https://ioi.gmbh/en/2024/02/14/creating-mock-services-for-test-automation-in-business-central.md"
excerpt: "Anyone planning to publish an extension on Microsoft’s AppSource will quickly find that they have to build their own mock services for test automation. Here is a very short guide you can adapt for your project if you use the..."
taxonomy_category:
  - "Tips &amp; tricks for developers"
---

Anyone planning to publish an Extension in Microsoft’s AppSource will quickly find that they have to build their own mock services for test automation. Here is a very short guide that you can adapt for your project if you use the built-in HTTP client to send or receive data to or from a web service via GET/ POST.

If you want to receive JSON data from a web service, you have to “fake” these calls, because **real HTTP connections are not allowed during tests**.

We assume that you have written a codeunit to set up the HTTP connection. If that is the case, you have already done a large part of the work. If not, you will probably need to restructure your code and move it into a separate codeunit. After that you are ready to go. Now for the crucial part: everywhere you call the codeunit function that calls the web service, you have to pass the object ID of the codeunit as well (for example through a field in a setup table and a table relation to “CodeUnit Metadata”.ID;). This allows your code to run either the real codeunit or the mock codeunit.

Since it is (in most cases) not possible to put the entire JSON (that is to be tested) into a single string, you have to create it “on the fly” yourself in your mock codeunit. Write a function that generates your JSON on the basis of a real JSON message from the web service. Leave out the unnecessary objects that your code will not process anyway.

You can construct your JSON message by creating variables in your function and using “JsonObject”, “JsonArray”, “JsonValue” and so on as required.

For this we create a text array and enter all the data.   
*Example*:  
If part of the JSON is the key/value pair “{“id”: “12345”}”, create the object as follows:  
`JsonValueArray[1] := '12345';`  
`JsonObject.Add('id', JsonValueArray[1]);`

If we have several entries in one JSON message, we use a 2-dimensional text array and modify the code slightly to loop through it:

`JsonValueArray[1][1] := '12345';`  
`JsonValueArray[2][1] := '67890';`

Then inside the loop:  
`JsonObject.Add('id', JsonValueArray[i][1]);`

Of course this is only a small part of what you actually have to do. But we hope it gives you at least a little guidance on how to write your JSON mock services for Business Central tests, because Microsoft leaves you pretty much in the dark about what to do.

About the author

#### Sascha Marquardt

Responsible for the partner programme, the licensing business and commercial project delivery.

## From reading to action. Book your upgrade check.

In the free upgrade check we analyse your current Navision or NAV version and show you the direct path to Business Central – including effort, sequence and a fixed-price range.

[Book a free Upgrade-Check →](/en/business-central-upgrade-check/)
[Give us a quick call: +49 214 8402 3000](tel:+4921484023000)

✓ Directly from any Navision or NAV version

✓ Live test while the legacy system keeps running

✓ C/AL→AL conversion included

✓ Fixed-price range and timeline afterwards
