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
NLTNLT 

please help with writing test class for below apex class on user object in salesforce

public class UserRoleSegment {
    public static void UserRoleUpdate(List<User> lstUsers) {
        set<ID> lstNames = new set<ID>();
        for(User u:lstUsers){
            lstNames.add(u.userRoleId);
        }
        Map<Id,USERRole> userRoleMap= new Map<Id,USERRole>([SELECT Id,name from USERRole WHERE name like 'Team Rep%' and Id in:lstNames]);
        for(User u:lstUsers){      
            if(userRoleMap.containskey(u.userroleID)){
                if (u.Segment__c == 'Major Accounts')
                {
                    u.Quota_Funnel_Health__c = 30;
                    u.SSO_Objective__c = 1;
                    u.Target_Appointments_Set__c = 5;
                    u.Target_Appointments_Attended__c = 5;
                }
                else if (u.Segment__c == 'Govt Support')
                {   
                    u.Quota_Funnel_Health__c = 30;
                    u.SSO_Objective__c = 2;
                    u.Target_Appointments_Set__c = 4;
                    u.Target_Appointments_Attended__c = 4;
                }
                else
                {
                    u.Quota_Funnel_Health__c = 25;
                    u.SSO_Objective__c = 2;
                    u.Target_Appointments_Set__c = 10;
                    u.Target_Appointments_Attended__c = 10;
                }
            }
        }
    }
}
Amit Singh 1Amit Singh 1
Ok, Use below code.
@isTest
private class Test_UserRoleSegment{
    
    static testMethod void testData(){
        UserRole r = new UserRole(DeveloperName = 'MyCustomRole', Name = 'Team Repr');
        insert r;
        List<User> userList = new List<User>();
     User u = new User(
     ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
     LastName = 'last',
     Email = 'puser000@amamama.com',
     Username = 'puser000@amamama.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US',
     UserRoleId = r.Id,
     Segment__c='Major Accounts');
     
     User u1 = new User(
     ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
     LastName = 'last',
     Email = 'puser000@amamama11.com',
     Username = 'puser000@amamama11.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US',
     UserRoleId = r.Id,
     Segment__c='Govt Support');
     
     User u2 = new User(
     ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
     LastName = 'last',
     Email = 'puser000@amamama22.com',
     Username = 'puser000@amamama22.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US',
     UserRoleId = r.Id,
     Segment__c='TEst Accounts');
     
     userList.add(u);
     userList.add(u1);
     userList.add(u2);
     Test.startTest();
         UserRoleSegment.UserRoleUpdate(userList);
     Test.StopTest();
    }
}

Let me know if this helps :)

Thanks!
Amit