You need to sign in to do that
Don't have an account?
Ravi23
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();
}
}
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();
}
}
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
Add this line in your test class.
ABfutureclass.createABdetails();
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: []
Can you please check if this permission set's user license is same as this profiles user license?
Did not understand this what is permission set profile?
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.
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.
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.
Try this
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.