+ Start a Discussion
Mano sfdcMano sfdc 

Test Class to my Simple Apex Class

Hi Experts,

I'm new to test classes and I have created an Apex Class, some one please help me on creating Test Class. Thanks in Advance.



public class TestUserRoleController 
{
    public String getObjType() 
    {
        return String.valueOf(Account.sObjectType);
    }
    
    public UserRoleHelper.RoleNodeWrapper getRootNodeOfTree(Id roleOrUserId) 
    {
        return UserRoleHelper.getRootNodeOfUserTree(roleOrUserId);
    }

    public String getJsonString() 
    {
        String str = null;
        str = UserRoleHelper.getTreeJSON('00E7F000001qt7I');
        return str; 
    }
    
    public String selectedValues {get; set;}
  
}
Best Answer chosen by Mano sfdc
Maharajan CMaharajan C
Hi Mano,

Add one more Parameter get 100% coverage:

@isTest
public class TestUserRoleController_Test {
    @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 ;
        
        TestUserRoleController con = new TestUserRoleController();
        con.getObjType() ; 
        con.getRootNodeOfTree(ur.id) ;
        con.getJsonString();
        con.selectedValues='Test';
    }
}

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

All Answers

Raj VakatiRaj Vakati
@isTest
public class TestUserRoleController_Test {
    @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 ;
        
        TestUserRoleController con = new TestUserRoleController();
        con.getJsonString() ; 
        con.getRootNodeOfTree(ur.id) ;
        con.getJsonString();
    }
}

 
Raj VakatiRaj Vakati
Use this one 
@isTest
public class TestUserRoleController_Test {
    @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 ;
        
        TestUserRoleController con = new TestUserRoleController();
        con.getObjType() ; 
        con.getRootNodeOfTree(ur.id) ;
        con.getJsonString();
    }
}
Maharajan CMaharajan C
Hi Mano,

Add one more Parameter get 100% coverage:

@isTest
public class TestUserRoleController_Test {
    @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 ;
        
        TestUserRoleController con = new TestUserRoleController();
        con.getObjType() ; 
        con.getRootNodeOfTree(ur.id) ;
        con.getJsonString();
        con.selectedValues='Test';
    }
}

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
This was selected as the best answer