3.3 - Programmatic Sharing - Unit Tests For Programmatic Apex Sharing
3.3 - Programmatic Sharing - Unit Tests For Programmatic Apex Sharing
• System.Assert() Methods
• System.runAs() Method
• Make sure that the expected output is working as expected as per the code specs
• Be able to deploy into the Production environment! You cannot deploy your code to Production unless tested!
• Manually through the User Interface: important, but testing through the user interface is prone to errors, and will
not catch all of the use cases for your application.
• Automatically through Unit Tests: special classes and methods that are made for testing only, and that automate
the testing procedure
Unit tests are class methods that verify In Salesforce, Testing is REQUIRED for
whether a piece of code is working Deployment to Production environment!
properly.
Think of a test method as 'Apex code
that tests other Apex code.'
Why!?
• Salesforce runs in a multitenant environment, many orgs share the same instance
• For that, any Apex code should be tested before being deployed
• Apex code can only be written in a sandbox environment or a Developer org, not in production
• At least 75% of your entire Salesforce Organization Apex code must be covered by Unit Tests
• Test methods and test classes are not counted as part of Apex code coverage
• Are always flagged with keyword testMethod or @isTest annotation at the method definition level
• Must always be defined in a test class. This test class should be annotated with @isTest annotation
• testMethod
• @isTest
@isTest @isTest
Public class myTestClass { Public class myTestClass {
static testMethod void myTest() { @isTest
// code_block static void myTest() {
} // code_block
} }
}
• We still did not compare the actual result with the expected result!
• It’s very important to test for different inputs to ensure the quality of your code
Method Description
System.assert(condition, [msg] ) • Asserts that the specified condition is true.
• If it is not, error is returned that causes code execution to halt.
• Can take optional error message as extra argument
System.assertEquals • Asserts that the first two arguments (expected and actual) are the same.
(expected, actual, [msg] ) • If they are not, error is returned that causes code execution to stop
• Can take optional error message as extra argument
System.assertNotEquals • Asserts that the first two arguments (expected and actual) are different.
(expected, actual, [msg] ) • If they are the same, error is returned that causes code execution to stop
• Can take optional error message as extra argument
• These methods will separate the Governor Limits and will give a new fresh Governor Limit set right before Test execution
Test.startTest Test.stopTest
• Marks the point in your code when your test begins • Marks the point in your code when your test ends
• Used in conjunction with the stopTest method • Used in conjunction with the startTest method
• Each test method is allowed to call it only once • Each test method is allowed to call it only once
• The runAs method does NOT enforce user permissions or field-level permissions, only record sharing.
• The original system context is started again after all runAs test methods complete
• You can create new users with runAs even if your organization has no additional user licenses
Test.startTest();
InvoiceSharing.manualShareRead(inv.Id, user1Id);
Test.stopTest();
…
Copyright © Walid El Horr 19
Test Class Example – Test Class part 2
Sharing Test Class – part 2
…
List<Invoice__Share> invShrs = [
SELECT Id, UserOrGroupId, AccessLevel, RowCause
FROM Invoice__Share
WHERE ParentId = :inv.Id AND UserOrGroupId= :user1Id
];
Demo
22
Summary - Unit Tests for Programmatic Apex Sharing
Subject Description