+ Start a Discussion
Ravi23Ravi23 

Test Class passing but not covering the Parent class always the coverage is 0%

Hi All I have a futre class and I have written a test class for the same which is passing but the parent class is just not covering any percent. Can some one hellp. Below is class and the test class.

Class
global class ABfutureclass {
   
  
    @future
    public static void createABdetails(){
          string timePSetName=Label.Time;
       PermissionSet timePSet =[SELECT Id,IsOwnedByProfile,Label FROM PermissionSet where Name=:timePSetName limit 1];
        Id myID2 = userinfo.getUserId();
        string permissionsetid = timePSet.id; 
        List<PermissionSetAssignment> lstPsa = [select id from PermissionsetAssignment where PermissionSetId =: permissionsetid AND AssigneeId =:myID2];
    
        if(lstPsa.size() == 0)
        {
            PermissionSetAssignment psa1 = new PermissionsetAssignment(PermissionSetId= permissionsetid, AssigneeId = myID2 );
            database.insert(psa1);
        }
    }

}



Test Class

@isTest
public class ABfutureclasstest {
    
    public static testMethod void createABdetails(){
        test.startTest();
        Profile Profile1 = [SELECT Id, Name FROM Profile WHERE Name = 'Basic Profile'];
       
     
      User u = [SELECT Id FROM User WHERE ProfileId =:Profile1.Id AND isActive = true LIMIT 1];
        PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name =: Label.Time];
        system.runAs(u){
   PermissionSetAssignment psa = new PermissionSetAssignment();
        psa.AssigneeId = u.Id;
        psa.PermissionSetId = ps.Id;
        insert psa;
        }
        test.stopTest();
    }

}
Best Answer chosen by Ravi23
sachin kadian 5sachin kadian 5
@isTest
public class ABfutureclasstest {
    
    public static testMethod void createABdetails(){
        
        Profile Profile1 = [SELECT Id, Name FROM Profile WHERE Name = 'Customer Community Login User'];
       
        Account ac = new Account(name ='sachin') ;
        insert ac; 
       
        Contact con = new Contact(LastName ='testCon',AccountId = ac.Id);
        insert con;  
                  
        User u = new User(alias = 'test123', email='test123@noemail.com',
                emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
                localesidkey='en_US', profileid = Profile1.id, country='United States',IsActive =true,
                ContactId = con.Id,
                timezonesidkey='America/Los_Angeles', username='tester@noemail.com');
       
        insert u;
     	
      	system.runAs(u){
        test.startTest();
        ABfutureclass.createABdetails();
        test.stopTest();
   		
        }
        
    }

}

Ravi try like this.. Dont assign the permission set in test class. when you will call the class it will execut the if part and will assign the permission set.

All Answers

GauravTrivediGauravTrivedi
Try to run this test class from developer console and before running select Always run Asynchronously, as there is know bug with regards to code coverage. if you didnt get code coverage let me know.
sachin kadian 5sachin kadian 5
You are not calling the class anywhere in your test class so you can expect it will cover the class.

Add this line in your test class.
ABfutureclass.createABdetails();
 
Ravi23Ravi23
Thanks Sachin. I called the class and now it covers the Parent Class completely but however the test class fails with the below error. Can you help please.

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Can't assign permission set to this user, user license doesn't match: []
sachin kadian 5sachin kadian 5
Hi Ravi,

Can you please check if this permission set's user license is same as this profiles user license?
Ravi23Ravi23
Hi Sachin,

                Did not understand this what is permission set profile?
Ravi23Ravi23
however license types of permsiiion set and profile both are same
sachin kadian 5sachin kadian 5
Permission Set user license

I mean the user license you selected when you created this permission set.

Please see this user license and then see salesforce license for the profile "Basic Profile". These both license should be same.
Ravi23Ravi23
Hi Sachin,

                Yes both of them(Permission set user license and profile user license) are of Customer Community Login User License type. Not understanding why I am getting this error.
Ravi23Ravi23
Hi Sachin,

                As I said it is passing but not covering even 1% please check the coverage in that case it is always 0%. But if I call the future class then test calss fails with the error message. 
sachin kadian 5sachin kadian 5
yes sorry my mistake. i forgot to call the method.. :)
Ravi23Ravi23
Sachin I am thinking because may be in the parent Class I gave userinfo.getuserid right thats why it is unable to assign or any other taughts how to overcome this situation?
sachin kadian 5sachin kadian 5
@isTest
public class ABfutureclasstest {
    
    public static testMethod void createABdetails(){
        Profile Profile1 = [SELECT Id, Name FROM Profile WHERE Name = 'Customer Community Login User'];
       
        Account ac = new Account(name ='sachin') ;
        insert ac; 
       
        Contact con = new Contact(LastName ='testCon',AccountId = ac.Id);
        insert con;  
                  
        User u = new User(alias = 'test123', email='test123@noemail.com',
                emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
                localesidkey='en_US', profileid = Profile1.id, country='United States',IsActive =true,
                ContactId = con.Id,
                timezonesidkey='America/Los_Angeles', username='tester@noemail.com');
       
        insert u;
     	
      	PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name =: 'new_Permission'];
        system.runAs(u){
   		PermissionSetAssignment psa = new PermissionSetAssignment();
        psa.AssigneeId = u.Id;
        psa.PermissionSetId = ps.Id;
        insert psa;
        test.startTest();
        ABfutureclass.createABdetails();
        test.stopTest();
        }
        
    }

}

Try this
Ravi23Ravi23
Thanks Sachin works perfectly. Last doubt since my test class has nothing to check the list size==0 in the parent class that block is not covering. Can you help me in that block also getting covered
Ravi23Ravi23
I made few modifications and it works now perfectly thanks a lot Sachin again for the help
sachin kadian 5sachin kadian 5
@isTest
public class ABfutureclasstest {
    
    public static testMethod void createABdetails(){
        
        Profile Profile1 = [SELECT Id, Name FROM Profile WHERE Name = 'Customer Community Login User'];
       
        Account ac = new Account(name ='sachin') ;
        insert ac; 
       
        Contact con = new Contact(LastName ='testCon',AccountId = ac.Id);
        insert con;  
                  
        User u = new User(alias = 'test123', email='test123@noemail.com',
                emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
                localesidkey='en_US', profileid = Profile1.id, country='United States',IsActive =true,
                ContactId = con.Id,
                timezonesidkey='America/Los_Angeles', username='tester@noemail.com');
       
        insert u;
     	
      	system.runAs(u){
        test.startTest();
        ABfutureclass.createABdetails();
        test.stopTest();
   		
        }
        
    }

}

Ravi try like this.. Dont assign the permission set in test class. when you will call the class it will execut the if part and will assign the permission set.
This was selected as the best answer
Ravi23Ravi23
Sure Sachin works perfectly. Thanks a lot for guiding me completely.