You need to sign in to do that
Don't have an account?

Hi Everyone, Please help me to get 100% code coverage for my test class, first else condition in my test class is not getting covered.
Apex class
global class ModelLevel{
global static String getfunction(){
String fpmsg;
User usr = [select Id,Name,accountid,c_User_Country__c From User where Id = :UserInfo.getUserId() LIMIT 1];
List<ObjectA__c> rcmsg = new List<ObjectA__c>();
rcmsg =[SELECT body__c from ObjectA__c where Type__C='homemessage' AND Country__c=:usr.c_User_Country__c LIMIT 1];
if(rcmsg!=null && !rcmsg.isEmpty() ){
if(rcmsg[0].body__c!=null && rcmsg[0].body__c!=''){
fpmsg =rcmsg[0].body__c;
}
else{
rcmsg =[SELECT id,name,Title__c,body__c from ObjectA__c where Type__C='homemessage' AND Country__c ='Global' LIMIT 1];
fpmsg= rcmsg[0].body__c;
}
}
else{
rcmsg =[SELECT id,name,Title__c,body__c from ObjectA__c where Type__C='homemessage' AND Country__c ='Global' LIMIT 1];
fpmsg= rcmsg[0].body__c;
}
return fpmsg.unescapeHtml4();
}
########################################################
Test class
@isTest
Global class Modelleveltest
{
private static testMethod void test_getfunction (){
Account acc = CreateTestDataUtility.createAccount();
Contact con = CreateTestDataUtility.createContact(acc);
Contact con1 = CreateTestDataUtility.createContact(acc);
Profile p = [SELECT Id FROM Profile WHERE Name = 'Sales User' LIMIT 1];
User localUser= new User
profileId = p.id,
username = 'Test33@test.com.test',
email = 'Test33@test.com.test',
emailencodingkey = 'UTF-8',
localesidkey = 'en_US',
languagelocalekey = 'en_US',
timezonesidkey='America/Los_Angeles',
alias='test22',
lastname='Testing22',
contactId=con.id,
country = 'Turkey',
Geographic_Region__c='Europe',
c_User_Country__c = 'Turkey',
User_Region__c = 'Turkey'
);
insert localUser;
User TestUser= new User(
profileId = p.id,
username = '23@test.com.test',
email = '23@test.com.test',
emailencodingkey = 'UTF-8',
localesidkey = 'en_US',
languagelocalekey = 'en_US',
timezonesidkey='America/Los_Angeles',
alias='newtest3',
lastname='Testing3',
contactId=con1.id,
country = 'France',
Geographic_Region__c='Europe',
c_User_Country__c = 'France',
User_Region__c = 'France'
);
insert TestUser;
List< ObjectA__c > Newval = new List< ObjectA__c >();
ObjectA__c usr1 = new ObjectA__c (Name='Content11',Country__c='Turkey',Customer_Type__c='Distributor',Title__c='Test Content',Body__c='Testconentnew',Type__c=' homemessage ');
Newval.add(usr1);
ObjectA__c usr2 = new ObjectA__c (Name='Content22',Country__c='Global',Customer_Type__c='Distributor',Title__c='Test Content test',Body__c='TestContentnewmsg',Type__c=' homemessage ');
Newval.add(usr2);
insert Newval;
Test.StartTest();
System.runAs(localUser){
ModelLevel. getfunction ();
}
System.runAs(TestUser){
ModelLevel. getfunction ();
}
Test.StopTest();
}
}
Please change the code in place which I mention earlier
Test this and let us know the results. If not works try another solution.
Thanks,
Kamala
All Answers
Please add this line after the Newval.add(usr2); in test class.
and then run the test class.
It will achieve the code to 100% test coverage.
Let us know if you have any questions.
Thanks,
Kamala. K
Sweet Potato Tec.
I tried the code which you have sent, but 1st else is not getting covered, that line is showing red
Please change the code in place which I mention earlier
Test this and let us know the results. If not works try another solution.
Thanks,
Kamala