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
sfdc_1sfdc_1 

How to create Test class with code coverage of 75% in salesforce?

can some one help me to write test class for below code as i am new to salesforce so i don't have much idea related to test class.

public class permissionset {
public void testOCRmethod() {
List < PermissionSetAssignment > permissionSetList = new List < PermissionSetAssignment > ();
Id permissionSetId = [SELECT id FROM permissionSet WHERE Name = 'test1per'].id;
Set < String > profileSet = new Set < String > { 'test1' };
for (User u: [SELECT ID, UserRole.Name, Profile.Name, IsActive FROM User WHERE Profile.Name IN: profileSet AND IsActive = true])

PermissionSetAssignment psa = new PermissionSetAssignment(PermissionSetId = permissionSetId, AssigneeId = u.Id);
permissionSetList.add(psa);
}
upsert permissionSetList;
}
}
permi
Earl Newcomer 10Earl Newcomer 10
Here is a test class that covers 100%.  I've been working on a similar class.  I rewrote your class just a bit and I'll include that too.
 
@isTest
public class permissionSetTest {

    @isTest 
    static void validatePermissionSetTest()
    {
        Test.startTest();
            
        PermissionSet.addPermissionSet();
        
		List<PermissionSetAssignment> psa = new List<PermissionSetAssignment>([select id, assignee.name, assignee.userrole.name, assignee.profile.name, assignee.isactive from permissionsetassignment where assignee.profile.name in ('Integration Administrator') and assignee.isactive = true and permissionset.name = 'testps1']); 
        
        system.AssertEquals(True, psa.size() == 2);
        
        Test.stopTest();
    }
}
Here is the slightly modified class.
public class permissionSet {
    public static void addPermissionSet() {
        // Create a list of permission list assignments.
        List<permissionSetAssignment> permissionSetList = new List <PermissionSetAssignment> ();
        
        // Get the permission set id by name.
        Id permissionSetId = [Select id from permissionset where name = 'testps1'].id;
        
        // Create a string variable to use to find users to assign to the permission set.
        Set<String> profileSet = new Set<String>{'Integration Administrator'};
        
        // Get users by the profile name (2 users in this case).
        for (User u : [select id, userrole.name, profile.name, isactive from user where profile.name in :profileset and isactive = true])
	    {
          	// Create the new permission set assignment instance.
          	PermissionSetAssignment psa = new permissionsetassignment(PermissionSetId = permissionSetId, AssigneeId = u.id);
            // Add the new permission set assignment to the psa list.
            permissionSetList.add(psa);
    	}
        // Apply the psa.
        upsert permissionSetList;
    }
}

100% test coverage