You need to sign in to do that
Don't have an account?
Mano 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;}
}
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;}
}
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
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