Exercise - Test a review-policy customization
In this exercise, you create the review-policy extension and its test app. You publish both apps to a Business Central online sandbox, run the tests twice, introduce a defect, and confirm that the focused suite detects it.
Prerequisites
You need Visual Studio Code with the AL Language extension, access to a Business Central online sandbox, and permission to publish development extensions. The sandbox also needs the Test Tool and the library that supplies Library Assert.
Arrange the version-compatible Test Tool, assertion library, and selected standard application tests before starting the timed exercise. Use a disposable company and administrator-approved test data and integrations. The account must be able to publish both development apps, execute the test infrastructure and custom objects, and use the exercise table. Record the product version, language, installed test-app versions, and relevant configuration; exact-message checks require a controlled language baseline.
Important
Use a sandbox company that is safe for test data. Don't perform this exercise in a production environment.
Create the production app
In Visual Studio Code, run AL: Go! and create a project named
ReviewPolicyfor your sandbox.In
app.json, keep the generated appid, setnametoReview Policy, setpublisherto your chosen publisher, and setversionto1.0.0.0.Set the app's object range to 71000 through 71049. Verify that its
platform,application, andruntimevalues are compatible with the target sandbox and downloaded symbols; don't assume that generated defaults match every environment.Delete the generated sample object.
Add the following table as
ReviewPolicyEntry.Table.al.table 71000 "Review Policy Entry" { DataClassification = CustomerContent; fields { field(1; "Entry No."; Integer) { AutoIncrement = true; DataClassification = SystemMetadata; } field(2; "Review Comment"; Text[100]) { DataClassification = CustomerContent; } field(3; Reviewed; Boolean) { DataClassification = CustomerContent; } } keys { key(PK; "Entry No.") { Clustered = true; } } }Add the following codeunit as
CustomerReviewPolicy.Codeunit.al.codeunit 71001 "Customer Review Policy" { procedure MarkReviewed(var ReviewEntry: Record "Review Policy Entry") begin if ReviewEntry."Review Comment" = '' then Error(MissingCommentErr); if not Confirm(ConfirmReviewQst, false) then exit; ReviewEntry.Reviewed := true; ReviewEntry.Modify(); end; var MissingCommentErr: Label 'Enter a review comment before completing the review.'; ConfirmReviewQst: Label 'Complete this review?'; }Run AL: Publish and confirm that the
Review Policyapp appears in Extension Management.
Verification point: The app publishes without test-library dependencies. Its table uses object 71000, and its codeunit uses object 71001.
Create the test app
In the sandbox, inspect the installed apps in Extension Management. Ask the administrator to install the version-compatible test framework, Test Tool runner, and required test libraries through the supported deployment process for that environment.
Verify that Test Tool opens and that the installed assertion-library app exports
System.TestLibraries.Utilities."Library Assert", as used by the examples. Standard application tests and their dependencies might require separate installation. Don't assume that an environment has a Configure Package action or that a generic runtime package is the supported online installation route.Create a second AL project named
ReviewPolicyTests, and add both projects to the same Visual Studio Code workspace.In the test app's
app.json, keep its generated appid, setnametoReview Policy Tests, and use object range 71050 through 71099.Keep the test app's generated ID distinct from the production app's ID. Verify its platform, application, and runtime compatibility too, and delete its generated sample object before adding the test codeunit. Reserve both object ranges so they don't collide with other installed training apps.
Add a dependency on
Review Policyby copying the production app's exactid,publisher,name, andversionvalues.From the installed assertion-library app or its downloaded symbol package, identify the
Library Assertcodeunit and its namespace. Copy that app's exact dependency record instead of using an ID from sample content or a different release.Add both records to the
dependenciesarray. Replace the uppercase tokens in this shape with the values you verified."dependencies": [ { "id": "REVIEW_POLICY_APP_ID", "publisher": "YOUR_PUBLISHER", "name": "Review Policy", "version": "1.0.0.0" }, { "id": "INSTALLED_LIBRARY_APP_ID", "publisher": "INSTALLED_LIBRARY_PUBLISHER", "name": "INSTALLED_LIBRARY_NAME", "version": "INSTALLED_LIBRARY_VERSION" } ]This is a manifest fragment, not a complete
app.json. Merge the two verified records into the test project's existingdependenciesarray; keep its other settings. The uppercase strings are placeholders, not usable app IDs or installed package versions.Run AL: Download Symbols. Confirm that symbols download for the production app and assertion library.
Add the following codeunit as
ReviewPolicyTests.Codeunit.al.using System.TestLibraries.Utilities; codeunit 71050 "Review Policy Tests" { Subtype = Test; TestPermissions = Disabled; var LibraryAssert: Codeunit "Library Assert"; ReviewPolicy: Codeunit "Customer Review Policy"; MissingCommentErr: Label 'Enter a review comment before completing the review.'; [Test] [TransactionModel(TransactionModel::AutoRollback)] [HandlerFunctions('AcceptReviewHandler')] procedure ReviewWithCommentSucceeds() var ReviewEntry: Record "Review Policy Entry"; begin CreateReviewEntry(ReviewEntry, 'Checked against the source document.'); ReviewPolicy.MarkReviewed(ReviewEntry); ReviewEntry.Get(ReviewEntry."Entry No."); LibraryAssert.IsTrue(ReviewEntry.Reviewed, 'The entry must be reviewed.'); end; [Test] [TransactionModel(TransactionModel::AutoRollback)] procedure ReviewWithoutCommentFails() var ReviewEntry: Record "Review Policy Entry"; begin CreateReviewEntry(ReviewEntry, ''); ClearLastError(); AssertError ReviewPolicy.MarkReviewed(ReviewEntry); LibraryAssert.AreEqual( MissingCommentErr, GetLastErrorText(), 'The policy returned an unexpected error.'); end; [Test] [TransactionModel(TransactionModel::AutoRollback)] [HandlerFunctions('DeclineReviewHandler')] procedure DeclinedReviewRemainsOpen() var ReviewEntry: Record "Review Policy Entry"; begin CreateReviewEntry(ReviewEntry, 'Ready for review.'); ReviewPolicy.MarkReviewed(ReviewEntry); ReviewEntry.Get(ReviewEntry."Entry No."); LibraryAssert.IsFalse(ReviewEntry.Reviewed, 'The entry must remain open.'); end; local procedure CreateReviewEntry(var ReviewEntry: Record "Review Policy Entry"; Comment: Text[100]) begin ReviewEntry.Init(); ReviewEntry."Entry No." := 0; ReviewEntry."Review Comment" := Comment; ReviewEntry.Insert(); end; [ConfirmHandler] procedure AcceptReviewHandler(Question: Text[1024]; var Reply: Boolean) begin LibraryAssert.AreEqual('Complete this review?', Question, 'Unexpected confirmation.'); Reply := true; end; [ConfirmHandler] procedure DeclineReviewHandler(Question: Text[1024]; var Reply: Boolean) begin LibraryAssert.AreEqual('Complete this review?', Question, 'Unexpected confirmation.'); Reply := false; end; }
Verification point: The test project compiles only after both verified dependencies resolve. The production app uses objects 71000 and 71001, and the test app uses object 71050.
Run the focused suite
Publish the test app to the same sandbox as the production app.
Open Test Tool, create a suite named
REVIEW POLICY, and select Get Test Codeunits.Add codeunit 71050,
Review Policy Tests, to the suite.Run the complete suite and confirm that all three methods pass.
Run the suite again without deleting or editing records.
Confirm that the second run contains the same three passing results.
Verification point: Two consecutive runs contain the same three passing custom test methods without manual data preparation. Explicit automatic rollback prevents test records from leaking between methods or runs; generated entry numbers don't need to match across runs.
Run a relevant standard-test baseline
- With the administrator, identify a small installed standard customer-area test subset appropriate to the scenario and the test company's data. Verify its codeunit IDs and prerequisites from the installed app's inventory or manifest; don't invent a range. Matching standard test apps and any required evaluation data must be available before this check can be completed.
- In Test Tool, create a separate suite named REVIEW POLICY BASELINE and add only that verified subset. Record why it was selected. This is a contextual standard-behavior baseline, not proof that the standalone review table integrates with Customer.
- Run this baseline suite twice without manually repairing data between runs. Record the exact methods, application and test-app versions, outcomes, and first actionable errors. Keep the REVIEW POLICY custom suite unchanged at three methods.
- Investigate failures from the smallest scope and distinguish missing prerequisites or pre-existing failures from changes introduced by the extension. A repeatable failure is still a failure, not a passing release result. Preserve the standard and custom reports separately; don't claim a standard-test run that couldn't be performed.
Detect and correct a defect
In
CustomerReviewPolicy.Codeunit.al, change the validation from= ''to<> ''.Important
This reversed condition is an intentional defect for the test-detection check only. Don't publish or retain it as the final implementation. Keep the expected test outcomes unchanged so the tests expose the defect instead of accommodating it.
Publish the production app, and rerun the
REVIEW POLICYsuite.Confirm that the suite fails and that the result identifies behavior affected by the reversed condition.
Depending on the method, the reversed condition can surface as the wrong validation error or an unexpected confirmation in the negative case. Don't add a handler to mask that changed path. Preserve the failure details and correct the production condition.
Restore the condition to
= '', and publish the correction.Rerun the failing method, codeunit, and complete suite in that order.
Record the extension versions, suite scope, initial failure, cause, correction, and final result.
If development publishing reuses the app version, also record a build identifier or timestamp and which condition was published so the defective and corrected builds remain distinguishable.
Verification point: The test suite detects the intentional defect, and all three tests pass after the correction.
After the narrow-to-broad reruns, run the complete custom suite twice without manually repairing records and confirm the same three passing methods in both runs. Keep the documented standard-test selection and its results with the release evidence. Restore the correct production condition before uninstalling or retaining either exercise app.
Troubleshoot common failures
A dependency can't be resolved: Compare all four dependency fields with the installed app manifest, confirm that versions are compatible, and download symbols again.
Library Assert** is unknown:** Confirm that the assertion library app is installed and that the test app references the package that exports the codeunit.Codeunit 71050 doesn't appear: Publish the test app, confirm
Subtype = Test, and refresh the suite by getting test codeunits again.A confirmation isn't handled: Confirm that the handler name exactly matches the name in
HandlerFunctionsand that the signature uses the required handler parameters.The second run reports a duplicate key: Verify that the published table uses
AutoIncrement = true, the helper requests a generated number with zero, and each complete test method usesAutoRollback. Review the runner configuration and any added fixture code; don't delete unrelated records to hide the failure.The error assertion has unexpected text: Clear the previous error before
AssertError, and compare the expected message with the stable application error that the policy raises.
For more guidance, see Test the advanced sample extension and Test codeunits and test methods.