You need to sign in to do that
Don't have an account?
Siddhesh Chavan
Test class not covering for loop
Hi all,
My test class is not covering the code inside for loop.
My test class is not covering the code inside for loop.
Apex class method:Class Name SampleClass public PageReference caseMethod() { List<case> updateCase = new List<case>(); NewComment = new List<CaseComment>(); childattachment = new List<Attachment>(); for(Case cs: selectedCasesComm){ if(cs.id != idRadio){ // Querty to retrive child attachment childattachment = [Select Id, ParentId, Body, Name from Attachment Where ParentId =: cs.id]; cs.ParentId = idRadio; cs.Status = 'Closed as Duplicate'; updateCase.add(cs); //Attachment Merging for(Attachment childatt : childattachment) { Attachment masterattachment = New Attachment(Name = childatt.Name, Body = childatt.Body); masterattachment.ParentId = idRadio; insert masterattachment; } delete childattachment; //Deleting old case attachments } } update updateCase; PageReference pageReference = new PageReference('/'+500+'/o'); pageReference.setRedirect(true); return pageReference; } Test Class public static testMethod void testcaseMethod(){ test.startTest(); SampleClass s= new SampleClass (); Case c = getTestCase(); Case cDup = getTestCaseDup(); Attachment cattachment = getTestCaseAttachment(); Attachment cDupattachment = getTestCaseAttachmentDup(); List<Case> mergeCasesmethod = new List<Case>(); List<Attachment> mergeCaseattachment = new List<Attachment>(); mergeCasesmethod.add(c); mergeCasesmethod.add(cDup); mergeCaseattachment.add(cattachment); mergeCaseattachment.add(cDupattachment); s.selectedCasesComm = mergeCasesmethod; // Working s.childattachment = mergeCaseattachment; //Not covered s.caseMethod(); test.stopTest(); }
Can you explain which line your test class is covering . One thing that I saw in your code which you shoiuld always take care of because
there is a query and DML inside for loop which will throw exception in near future .
Please check the code and check in test class may be the list you are using in for loop may not have record .
Create some test data for case , Attachment objects.
Please refer the below link:
https://developer.salesforce.com/forums/ForumsMain?id=906F00000008yzKIAQ
Thanks,
Shiva
Thank you for reply,
@Manoj : Code inside for loop in not covering
@Shiva: I have already insert test data for case and attachment in test class before
testcaseMethod method.
static Case getTestCase(){
Case newcase = new Case();
newcase.Subject = 'Test Subject';
newcase.Origin = 'Email';
newcase.Status ='New';
newcase.Description = 'SFDC Test case';
insert newcase;
return newcase;
}
static Case getTestCaseDup(){
Case newMergecase = new Case();
newMergecase.Subject = 'Test Subject';
newMergecase.Origin = 'Email';
newMergecase.Status ='New';
newMergecase.Description = 'SFDC Test case';
insert newMergecase;
return newMergecase;
}
static Attachment getTestCaseAttachment(){
Case c = getTestCase();
Attachment att = new Attachment();
att.ParentId = c.Id;
att.name = 'test';
att.body = blob.valueof('test');
insert att;
return att;
}
static Attachment getTestCaseAttachmentDup(){
Case cDup = getTestCaseDup();
Attachment attDup = new Attachment();
attDup.ParentId = cDup.Id;
attDup.name = 'test1';
attDup.body = blob.valueof('test1');
insert attDup;
return attDup;
}