Thursday, November 20, 2025

Latest Mocking HTTP Calls in AL Business Central (on-premise) for Better Testing

Hi All,


In Business Central development, talking to external web services using `HttpClient` is common. But when you write automated tests for AL code, relying on real HTTP calls is brittle as the external service could be down, change its API, or slow you down. That’s why the ability to mock outbound HTTP calls in AL is a big deal.


What’s the New Feature?

In recent Business Central versions, AL lets you define a test handler for HTTP calls in test codeunits. You can intercept `HttpClient` requests (GET, POST, etc.) and simulate responses. You also control how outgoing calls are handled with a property called `TestHttpRequestPolicy`.

`TestHttpRequestPolicy` has different modes:

  • BlockOutboundRequests: Throws an error if a request goes out that your handler doesn’t explicitly cover.
  • AllowOutboundFromHandler: Only requests handled by your function are allowed.
  • AllowAllOutboundRequests: Everything is allowed, but you can still handle specific ones.

This means your tests no longer depend on external systems.


Why This Is Useful

First, your tests become more stable and predictable. You decide the response for a given request, so your tests won’t fail because of network issues or API changes.

Second, it’s faster. Instead of waiting for real HTTP calls, your tests run with mocked responses.

Third, you can simulate different scenarios (success, error, timeouts) and verify how your AL code handles them.

Now, you will ask what's the point if we cannot test the REAL API. Well I see your point!

But think about this:

  1. Test your AL logic independently: You can verify that your code correctly handles the HTTP response, parses JSON, or reacts to different status codes.

  2. Simulate different scenarios: For example, you can test:

    • Successful response (200 OK)

    • Failed response (500 Internal Server Error)

    • Timeout or network error

  3. Avoid dependencies: Your tests don’t fail just because the API is down, slow, or returns unexpected data in production.

  4. Faster tests: No waiting for network calls.

So mocking is about testing your Business Central AL logic safely, not testing the third-party API.


A Simple Example

Here’s how you might write a test for a codeunit that fetches data from a web service:


codeunit 50100 MyService { procedure GetData(): Text var Client: HttpClient; Response: HttpResponseMessage; Content: Text; begin if Client.Get('https://api.example.com/data', Response) then
        begin Response.Content.ReadAs(Content); exit(Content); end else Error('HTTP request failed'); end; }

codeunit 50200 MyServiceTests { Subtype = Test; TestHttpRequestPolicy =
TestHttpRequestPolicy::AllowOutboundFromHandler; [Test] [HandlerFunctions('HandleHttp')] procedure TestGetData() var Service: Codeunit "MyService"; Result: Text; begin Result := Service.GetData(); if Result <> 'Mocked response' then Error('Unexpected result: %1', Result); end; [HttpClientHandler] procedure HandleHttp(Request: TestHttpRequestMessage;
var Response: TestHttpResponseMessage): Boolean begin // Intercept GET requests and return a mocked response if Request.RequestType = HttpRequestType::Get then begin Response.Content.WriteFrom('Mocked response'); Response.HttpStatusCode := 200; exit(false); // Do not call the real service end; exit(true); // Allow other requests if not handled end; }



In this test:

  • We simulate a GET request to `https://api.example.com/data`.
  • Our `HandleHttp` function returns a fake response, “Mocked response”.
  • The test checks that `GetData` returns that mocked content.

Things to Keep in Mind

  • This mocking feature works only in test codeunit. It doesn’t change behavior in production.
  • You need to set the correct `TestHttpRequestPolicy` so your handler is used and other calls are blocked or allowed.
Note:

Always set the property:

TestHttpRequestPolicy = TestHttpRequestPolicy::AllowOutboundFromHandler;

This ensures that all outbound HTTP calls in your test are intercepted by the handler.

  • This makes your tests more isolated: you don’t rely on external services, so they’re faster and more reliable.


Being able to mock HTTP calls in AL is a real game-changer for Business Central developers. It gives you more control over testing, reduces dependency on external systems, and helps you simulate different scenarios. If you write integration-style AL tests, this feature can save a lot of time and frustration.



Reach out to me if you have any questions or suggestions. 

Check out other blogs, if you haven't already. 

Kindly suggest what else you want me to blog about. Keep following and supporting!

You can also email me at jyotsnaoberainav@gmail.com

Also, visit my website JONAV if you are looking for NAV/D365BC Training and/or Services!

Thank you!

No comments:

Post a Comment