+ Start a Discussion
arunadeveloperarunadeveloper 

Unable cover my apex class in test class need help

Below Red lines are not covering in my test class

 

global class ServiceRequestUpdateFieldsAndReTypeClass {
public static string exceptionMessage='NoException';
public ServiceRequestUpdateFieldsAndReTypeClass(){

}
WebService static string udateServiceRquestFields(Id caseIdValue){
string message=updateCaseFields(caseIdValue);
return message;
}

public static string updateCaseFields(Id caseIdValue){

set<id> serviceRequestIdSet=new set<Id>();
set<id> usetIdSet=new set<id>();
list<ISERV_USER__c> caseUsersList=new list<ISERV_USER__c>();
boolean checkAdmin=false;
Integer countUsers=0;
list<Case> caseUpdateList=new list<Case>();
map<Id,list<ISERV_USER__c>> caseUserMap=new map<Id,list<ISERV_USER__c>>();

caseUsersList=[select id,SERV_REQST_ID__c,SERV_REQST_ID__R.Service_Request_Type__c
,ACCT_ADMIN_REGIS_AUTH_TXT__c,SERV_REQST_ID__R.CheckUserhasAccountAdmin__c
from ISERV_USER__c where
SERV_REQST_ID__c =: caseIdValue
and SERV_REQST_ID__R.Service_Request_Type__c='New'
and ACCT_ADMIN_REGIS_AUTH_TXT__c='Update'
];

if(caseUsersList.size()>0){

for(ISERV_USER__c caseUserObj: caseUsersList){
if(caseUserObj.SERV_REQST_ID__c!=null){
serviceRequestIdSet.add(caseUserObj.SERV_REQST_ID__c);

if(caseUserMap.containskey(caseUserObj.SERV_REQST_ID__c)){
caseUserMap.get(caseUserObj.SERV_REQST_ID__c).add(caseUserObj);
}else{
caseUserMap.put(caseUserObj.SERV_REQST_ID__c,new list<ISERV_USER__c>{caseUserObj});
}
}
}
checkAdmin=false;
for(ISERV_USER__c caseUserObjRec : caseUserMap.get(caseIdValue)){
if(caseUserObjRec.ACCT_ADMIN_REGIS_AUTH_TXT__c=='Update'){
++countUsers;
}
}
if(countUsers>1){
Case updateCase=new Case(Id=caseIdValue,SUBMT_BY_NM__c=userinfo.getFirstName()+' '+userinfo.getLastName(),SUBMT_TS__c=date.today(),CheckUserhasAccountAdmin__c=true);
caseUpdateList.add(updateCase);
exceptionMessage='Exception';
}
if(countUsers==1){
Case updateCase=new Case(Id=caseIdValue,Status='Submitted',SUBMT_BY_NM__c=userinfo.getFirstName()+' '+userinfo.getLastName(),SUBMT_TS__c=date.today(),CheckUserhasAccountAdmin__c=true);
caseUpdateList.add(updateCase);
}
}else{
Case updateCase=new Case(Id=caseIdValue,Status='Submitted',SUBMT_BY_NM__c=userinfo.getFirstName()+' '+userinfo.getLastName(),SUBMT_TS__c=date.today(),CheckUserhasAccountAdmin__c=true);
caseUpdateList.add(updateCase);
}
if(caseUpdateList!=null){
update caseUpdateList;
}
return exceptionMessage;
}

}

This is is the test clas

ServiceRequestUpdateFieldsAndReTypeClass classObj=new ServiceRequestUpdateFieldsAndReTypeClass();

Case newCase=new Case(Status='New',SUBMT_BY_NM__c=userinfo.getFirstName()+' '+userinfo.getLastName(),SUBMT_TS__c=date.today(),CLNT_DIV_QTY__c=2);
insert newCase;

list<ISERV_USER__c> iserviceUserList=new list<ISERV_USER__c>();

for(Integer i=0;i<3;i++){
ISERV_USER__c iserviceUser1=new ISERV_USER__c(Name='testName'+i,SERV_REQST_ID__c=newCase.id,ACCT_ADMIN_REGIS_AUTH_TXT__c='Update'
,USER_PHONE_TXT__c='1234567890',USER_EML_ADR__c='test@test.com',
VERIF_USER_EML_ADR__c='test@test.com',USER_JOB_TITLE_NM__c='testName'
,USER_FIRST_NM__c='TestFirstName',USER_LAST_NM__c='TestLastNAme');
iserviceUserList.add(iserviceUser1);
}

insert iserviceUserList;


map<id,list<ISERV_USER__c>> caseUserMap=new map<id,list<ISERV_USER__c>>();

caseUserMap.put(newCase.id,iserviceUserList);


test.startTest();
string message1=ServiceRequestUpdateFieldsAndReTypeClass.updateCaseFields(newCase.id);
string message=ServiceRequestUpdateFieldsAndReTypeClass.udateServiceRquestFields(newCase.id);
test.stopTest();

Case selectedRec=[select id,Status from Case where id=:newCase.id limit 1];
system.assertEquals('Submitted', selectedRec.Status);
system.assertEquals('NoException', message);

firechimpfirechimp

Hi arunadeveloper,

 

It may be because your query requires SERV_REQST_ID__R.Service_Request_Type__c='New' and you havn't set this in your test data.

 

Hope this helps!