+ Start a Discussion
Mano sfdcMano sfdc 

Apex Help - Test Class

Hi Experts,

I have created an Apex Class, some one please help me on creating Test Class. Thanks in Advance.



public class UserRoleController {
    
    public Boolean selectable {get; set;}
    
    public String selectNodeKeys {get; set;}

    {
        selectable = false;
        selectNodeKeys = 'No value selected';
    }
    
    public String JsonData {get; set;}
    
    public String roleOrUserId {get; set;}
    
    public String getJsonString() 
    {
        if (JsonData == null){
            JsonData = UserRoleHelper.getTreeJSON(roleOrUserId);
        }
        return JsonData;
    }

}
Maharajan CMaharajan C
Hi Mano;

Please try the below test class:

100 % Code Coverage:

@isTest
public class TestUserRoleController {

@isTest  static void testUserRoleHelper() {
        Profile pf = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
        
        UserRole ur = new UserRole(Name = 'CEO');
        insert ur;
        
        String orgId = UserInfo.getOrganizationId();
        String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
        
        Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
        String uniqueName = orgId + dateString + randomInt;
        User tuser = new User(  firstname = 'Test',
                              lastName = 'Test',
                              email = uniqueName + '@test' + orgId + '.org',
                              Username = uniqueName + '@test' + orgId + '.org',
                              EmailEncodingKey = 'ISO-8859-1',
                              Alias = uniqueName.substring(18, 23),
                              TimeZoneSidKey = 'America/Los_Angeles',
                              LocaleSidKey = 'en_US',
                              LanguageLocaleKey = 'en_US',
                              ProfileId = pf.Id,
                              UserRoleId = ur.Id);
        
        insert tuser ;
        
        UserRoleController  URCont = new UserRoleController () ;
        URCont.roleOrUserId = ur.Id ;
        URCont.selectable = false;
        URCont.selectNodeKeys = 'No value selected';
        URCont.getJsonString();
        URCont.JsonData = 'null';
    }


}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Raj