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:
Test your AL logic independently: You can verify that your code correctly handles the HTTP response, parses JSON, or reacts to different status codes.
-
Simulate different scenarios: For example, you can test:
-
Successful response (
200 OK) -
Failed response (
500 Internal Server Error) -
Timeout or network error
-
-
Avoid dependencies: Your tests don’t fail just because the API is down, slow, or returns unexpected data in production.
-
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:
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.
- 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.
No comments:
Post a Comment