+ Start a Discussion
Yelper DevYelper Dev 

Permission Set Test Class Error

Hi badly need help, error says "System.NullPointerException: Attempt to de-reference a null object" for the following test class below.
 
@isTest static void getViewDataWithoutPermission() {
        getAllRecords();
        Test.startTest();
        User u = setUser(false);
        System.runAs(u) {
            List<Authorisation__c> auths = AuthorisedAccessForController.getViewData(acc2.Id);
            Integer count = 0;
            for (Authorisation__c autho : auths) {
                List<ProductAuthorisation__c> pas = autho.Product_Authorisations__r;
                if (pas != null && !pas.isEmpty()) {
                    count =+ pas.size();
                }
                system.debug('@@@TEST::count'+count);
            }
            Test.stopTest();
            system.debug('@@@TEST::AUTH'+auths);
            //System.assertEquals(1, auths.size());
            //System.assertEquals(1, count);
        }
    }
I modularized the User by creating a boolean class "setUser"
 
static User setUser(Boolean withPS) {
        Profile p = [Select Id from Profile where NAME = 'System Administrator'];
        
        User u = new User(
            ProfileId = p.Id,
            Username = 'testclass123@racv.com',
            Alias = 'tclass',
            Email='testclass123@racv.com',
            EmailEncodingKey='UTF-8',
            Firstname='Spongebob',
            Lastname='Squarepants',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            TimeZoneSidKey='America/Chicago');
			insert u;
	      
        
        if (withPS) {
            PermissionSet ps = [SELECT ID From PermissionSet WHERE Name = 'AuthorisationAccess'];
			insert new PermissionSetAssignment(AssigneeId = u.Id, PermissionSetId = ps.Id );
        }
           return u;
       
    }
Question, why is it returning null value?

This is my controller:
public static List<Authorisation__c> getViewData(String customerId) {
        List<Authorisation__c> dataList = new List<Authorisation__c>();
        
        try {
            dataList = Database.query(getQuery(customerId));
            
            
            if ((dataList == NULL || dataList.isEmpty()) && Test.isRunningTest()) {
                System.debug('Throwing exception');
                throw new mexException();
            }
        } catch (Exception e) {
            UTIL_LoggingService.logHandledException(e, ORG_ID, APP_NAME, CLASS_NAME, 'getViewData', null, LOGGING_LEVEL);
        }
        
        if(dataList.size() > 0) {
            return dataList;
        } else {
            return null;
        }
    }


 
Lokesh KumarLokesh Kumar
remove this line 
system.debug('@@@TEST::AUTH'+auths);

because in order to include this you need to first declare the List and then initialize. 
Yelper DevYelper Dev
What is the relation with the system.debug? Hehe still get the same error though