Newer Version Available
Permission Set Groups
To provide Apex test coverage for permission set groups, write tests using the calculatePermissionSetGroup() method in the System.Test class.
The calculatePermissionSetGroup() method forces an immediate calculation of aggregate permissions for a specified permission set group. As the forced calculation counts against Apex CPU limits, and can require complex data setup, it’s a best practice to minimize the number of times you perform this operation.
Set this test to run once in a Test setup method, then reuse the data in subsequent tests.
1@isTest public class PSGTest {
2 @isTest static void testPSG() {
3 // get the PSG by name (may have been modified in deployment)
4 PermissionSetGroup psg = [select Id, Status from PermissionSetGroup where DeveloperName='MyPSG'];
5
6 // force calculation of the PSG if it is not already Updated
7 if (psg.Status != 'Updated') {
8 Test.calculatePermissionSetGroup(psg.Id);
9 }
10
11 // assign PSG to current user (this fails if PSG is Outdated)
12 insert new PermissionSetAssignment(PermissionSetGroupId = psg.Id, AssigneeId = UserInfo.getUserId());
13
14 // additional tests to validate permissions granted by PSG
15 }
16}