Mock outbound HttpClient web service calls during testing

Important

Some of the functionality described in this release plan has not been released. Delivery timelines may change and projected functionality may not be released (see Microsoft policy). Learn more: What's new and planned

Enabled for Public preview General availability
Admins, makers, marketers, or analysts, automatically Apr 2025 Apr 2025

Business value

To allow for easier testability of AL code that calls outbound web services, we are introducing the ability to mock such calls. This allows the developer to hardcode return values to the calling code during tests, without actually performing the outbound web service call. The ability to mock calls is especially valuable when testing Copilot and AI features, as developers can write unit tests for these without actually connecting to Azure OpenAI Service, and thereby save any unnecessary token consumption. Note that the feature is limited to on-premises instances only, including containers used for automated testing.

Feature details

AL developers can easily mock out HttpClient Get/Put/Delete/Post calls in unit tests. The idea is to allow easy substitutions of the responses of HttpClient calls in unit tests by introducing a new handler function to intercept HttpClient calls.

When an HttpClientHandler is added to a test method, every HTTP request that occurs during the execution of that test will be intercepted and routed to the handler. The handler method signature is as follows: it receives a TestHttpRequestMessage that contains information about the HTTP request, as well as a TestHttpResponseMessage that contains the mocked HTTP response values that should be updated by the handler. The Boolean return value indicates whether to fall through and issue the original HTTP request (true) or to use the mocked response (false).

We have also introduced a new property on test codeunits called TestHttpRequestPolicy. This property determines how outbound HTTP requests are treated during test execution and has the following possible values:

  • BlockOutboundRequests: Any HTTP request issued during the test execution that is not caught and handled by an HTTP client handler will raise an exception.
  • AllowOutboundFromHandler: All HTTP requests issued during the test execution are required to be caught by an HTTP client handler. The handler is allowed to explicitly fall through to issue the original request to the external endpoint.
  • AllowAllOutboundRequests: All outbound HTTP requests issued during the test execution are allowed.

The following shows an example of a codeunit making an external web service call, as well as a test codeunit that intercepts and mocks this. Notice the use of the HttpClientHandler in the test codeunit.

Example of test case mocking the external web service call by using the HttpClientHandler

Example of code making external httpclient web call that we want to mock

Tell us what you think

Help us improve Dynamics 365 Business Central by discussing ideas, providing suggestions, and giving feedback. Use the forum at https://aka.ms/bcideas.