+ Start a Discussion
Siddhesh ChavanSiddhesh Chavan 

Test class not covering for loop

Hi all,

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';
                //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');
        return pageReference;

Test Class

public static testMethod void testcaseMethod(){
    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>();
   s.selectedCasesComm = mergeCasesmethod; // Working 
   s.childattachment  = mergeCaseattachment;  //Not covered  

Hey Siddhesh,

 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 .
shiva pendemshiva pendem
HI Siddhesh,

Create some test data for case , Attachment objects.

Please refer the below link:


Siddhesh ChavanSiddhesh Chavan
Hi Manoj and 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;