Apply the AL testing framework
The testing strategy defines what evidence the review policy needs. The AL testing framework supplies the execution model that turns those cases into repeatable tests inside Business Central.
Define test codeunits and methods
A test codeunit uses Subtype = Test. A global procedure marked with [Test] is a test method that the framework can discover and run. Keep production code and test code in separate apps. The production app remains deployable without test libraries, while the test app declares dependencies on the production app and any libraries it uses.
codeunit 71050 "Review Policy Tests"
{
Subtype = Test;
TestPermissions = Disabled;
[Test]
procedure ReviewWithCommentSucceeds()
begin
// Arrange, act, and assert.
end;
}
Note
The framework skeletons illustrate declarations and attributes, not complete behavioral tests. Don't add these partial examples alongside the complete objects with the same IDs. The exercise supplies the production table and codeunit and a separate, complete test codeunit.
TestPermissions determines how permission sets assigned to a test codeunit affect execution. Disabled runs the test without applying test permission restrictions. Other modes support permission-focused tests. Choose the value deliberately because a permissions failure and a business-rule failure answer different questions.
Under the standard test framework, TestPermissions = Disabled executes tests using SUPER rather than applying restrictive test permissions. This isn't an end-user-permissions check. Arrange deployment and test-infrastructure access separately, and add permission-focused tests when those rules are in scope.
Use names that describe behavior and expected outcomes. ReviewWithCommentSucceeds exposes intent in Test Tool results. A generic name such as Test1 requires a reader to inspect the implementation before understanding a failure.
Control transactions and isolation
Test methods run in a database transaction. The [TransactionModel(...)] attribute changes transaction behavior for a particular method:
AutoRollbackrolls back test changes when the method finishes and is the usual choice for isolated tests.AutoCommitallows committed changes and needs careful cleanup and ordering analysis.Nonedoesn't wrap the method in a test transaction.
With None, the test method can't write directly to the database; page interactions can start their own transactions. The complete tests here use explicit AutoRollback because the demonstrated policy doesn't commit.
[Test]
[TransactionModel(TransactionModel::AutoRollback)]
procedure MissingCommentIsRejected()
begin
// The framework rolls back records created by this method.
end;
A codeunit with Subtype = TestRunner can set TestIsolation to define isolation across the tests it runs. Function isolates each test method. Codeunit isolates each test codeunit. Disabled leaves isolation to transaction behavior. Test isolation can override transaction behavior, so configure the runner and test methods as one execution design rather than as unrelated settings.
codeunit 71051 "Review Policy Test Runner"
{
Subtype = TestRunner;
TestIsolation = Function;
}
This runner fragment illustrates TestIsolation; without execution logic it doesn't run a suite by itself. Use the installed Test Tool runner in the exercise rather than adding this illustrative custom runner.
The Test Tool provides its own runner behavior for suite execution. A custom test runner is useful when a delivery pipeline or specialized result processing requires one. Don't add a runner merely to duplicate Test Tool execution.
Replace user interaction with handlers
Test code doesn't display an interactive dialog. A handler function intercepts the corresponding application call, supplies a response, and optionally verifies the text. Attach the handler to a test method with HandlerFunctions.
[Test]
[HandlerFunctions('ConfirmReviewHandler')]
procedure ConfirmedReviewSucceeds()
begin
// Call production code that uses Confirm.
end;
[ConfirmHandler]
procedure ConfirmReviewHandler(Question: Text[1024]; var Reply: Boolean)
begin
Reply := true;
end;
AL provides handlers for dialogs and UI objects, including message, confirm, page, modal page, report, request page, filter page, and hyperlink interactions. Match the handler type and signature to the interaction. A test fails when code invokes a handler that isn't declared in HandlerFunctions, or when a declared handler isn't called. This behavior helps detect a changed interaction path.
Keep handlers narrow. If a confirm handler accepts every prompt, an unrelated confirmation can pass unnoticed. Verify distinctive prompt text when that text is part of the behavior under test.
Verify outcomes with assertion tools
For a negative case, place AssertError directly before the statement that must raise an error. Then inspect the captured error.
ClearLastError();
AssertError ReviewPolicy.MarkReviewed(ReviewEntry);
LibraryAssert.AreEqual(
MissingCommentErr,
GetLastErrorText(),
'The policy returned an unexpected error.');
The assertion fragment belongs inside a test codeunit. Its object file must import System.TestLibraries.Utilities to resolve the current Library Assert codeunit; the complete examples below include that import.
GetLastErrorText is useful when exact wording is stable and meaningful. If a platform error contains variable details, compare a stable portion or assert a more direct state change. Exact matching against incidental text makes tests fragile.
The Library Assert codeunit provides assertions such as equality and Boolean checks. It belongs to the Business Central test libraries, not the production runtime app. The test app must reference the installed library package that contains it. Dependency IDs and versions can vary by Business Central release and localization. Obtain each dependency record from the installed app information or the downloaded symbol package's app.json, and don't copy an unverified globally unique identifier (GUID) from an example.
See Test codeunits and test methods, TestPermissions property, and TestIsolation property for property details.