function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kevin Knopf 11Kevin Knopf 11 

Attempt to de-reference a null object Test Class

I am trying to write a test class for a trigger that copies a list of Permission Sets from an existing to a new user. My thought was to have my test class compare the new user and the mirror user Permission Set Assignments to pass my code coverage. However, when I query either the mirror user or the new user I am getting an "Attempt to de-reference a null object" every time.

When I check the logs the trigger works (it fires after insert), but when I get to mirrorPerm.addAll I get the de-refernce error. 

If it matters, when I create a new user outside of the debugger the Permission Sets assign correctly, so I assume the trigger is working. 

@istest
private class UserPermSetTest {
	public static List<PermissionSetAssignment> userPerm;
    public static List<PermissionSetAssignment> mirrorPerm;
    public static List<User> newUserList;
    
    
    /* Test creating a new user */
    static testMethod void testNewUserCreated(){
        
        Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name = 'System Administrator'];
        User mirror = [SELECT Id FROM User WHERE Name = 'Tyler Ditto'];
        
        
        user newUser = new User(FirstName = 'Johnny',
                                 LastName = 'Quest',
                                 Alias = 'JQuest',
                                 Email = 'JQuest@ptest.com',
                                 UserName = 'JQuest@ptest.com.kevink',
                                 CommunityNickname = 'JQuest',
                                 UserRoleId = r.Id,
                                 ProfileId = p.Id,                                 
                                 Hire_Date__c = date.today(),
                                 Mirror_User__c = mirror.Id,
                                 ManagerId = mirror.Id,
                                 TimeZoneSidKey = 'America/New_York',
                                 LocaleSidKey = 'en_US',
                                 emailencodingkey = 'UTF-8',
                                 LanguageLocaleKey = 'en_US');
        
        insert newUser;
        
        System.debug('******************************* USER ID ' + newUser.Id);
        System.debug('******************************* MIRROR ID ' + mirror.Id);
       
        mirrorPerm.addAll([SELECT Id FROM PermissionSetAssignment WHERE AssigneeId =: mirror.Id AND PermissionSet.IsOwnedByProfile = false]);
        mirrorPerm.sort();
        System.debug('******************************* MIRROR PERMISSION SET ' + mirrorPerm);
        userPerm.addAll([SELECT Id FROM PermissionSetAssignment WHERE AssigneeId =: newUser.Id AND PermissionSet.IsOwnedByProfile = false]);
        userPerm.sort();
        System.debug('******************************* USER PERMISSION SET ' + userPerm);
       
        System.assertEquals(userPerm.equals(mirrorPerm), true);
            
    }
}

 
Best Answer chosen by Kevin Knopf 11
Raj VakatiRaj Vakati
@istest
private class UserPermSetTest {
	public static List<PermissionSetAssignment> userPerm =  new List<PermissionSetAssignment>();
    public static List<PermissionSetAssignment> mirrorPerm = new List<PermissionSetAssignment> () ;
    public static List<User> newUserList = new List<User> ();
    
    
    /* Test creating a new user */
    static testMethod void testNewUserCreated(){
        
        Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name = 'System Administrator'];
        User mirror = [SELECT Id FROM User WHERE Name = 'Tyler Ditto'];
        
        
        user newUser = new User(FirstName = 'Johnny',
                                 LastName = 'Quest',
                                 Alias = 'JQuest',
                                 Email = 'JQuest@ptest.com',
                                 UserName = 'JQuest@ptest.com.kevink',
                                 CommunityNickname = 'JQuest',
                                 UserRoleId = r.Id,
                                 ProfileId = p.Id,                                 
                                 Hire_Date__c = date.today(),
                                 Mirror_User__c = mirror.Id,
                                 ManagerId = mirror.Id,
                                 TimeZoneSidKey = 'America/New_York',
                                 LocaleSidKey = 'en_US',
                                 emailencodingkey = 'UTF-8',
                                 LanguageLocaleKey = 'en_US');
        
        insert newUser;
        
        System.debug('******************************* USER ID ' + newUser.Id);
        System.debug('******************************* MIRROR ID ' + mirror.Id);
       
        mirrorPerm.addAll([SELECT Id FROM PermissionSetAssignment WHERE AssigneeId =: mirror.Id AND PermissionSet.IsOwnedByProfile = false]);
        mirrorPerm.sort();
        System.debug('******************************* MIRROR PERMISSION SET ' + mirrorPerm);
        userPerm.addAll([SELECT Id FROM PermissionSetAssignment WHERE AssigneeId =: newUser.Id AND PermissionSet.IsOwnedByProfile = false]);
        userPerm.sort();
        System.debug('******************************* USER PERMISSION SET ' + userPerm);
       
        System.assertEquals(userPerm.equals(mirrorPerm), true);
            
    }
}

Use the above code