function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sebastian PageSebastian Page 

Why Some line of the code is not covering


Hi All 

Why Some line is not covering code coverage .
User-added image

I am using test class here
@isTest
public class CaseMergeHandlerTest {
    
      static testMethod void testMerge(){        
       List<String>DupCase =new List<string>();
        Account testAccount = new Account(Name='Test Company Name123');
        insert testAccount;
       
         Contact con  = new Contact(LastName='Testcont' ,Email='sss@test.com', AccountId=testAccount.id);
        insert con;
          list<case>lstcase =new LIst<case>();
             Case caseObj1 = new Case();
            caseObj1.AccountId = testAccount.Id;
            caseObj1.contactId=con.id;
            caseObj1.Status = 'Action-Automated Escalation';
            caseObj1.Origin ='Email';
          lstcase.add(caseObj1);
          DupCase.add(caseObj1.id);
           Case caseObj2 = new Case();
            caseObj2.AccountId = testAccount.Id;
            caseObj2.contactId=con.id;
            caseObj2.Status = 'Closed Duplicate';
            caseObj2.Origin ='Email';
          lstcase.add(caseObj2);
          DupCase.add(caseObj2.id);
       test.startTest(); 
        insert lstcase;
        
          attachment attach=new attachment();    
          attach.name='test.html';   
         Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=lstcase[0].id;
        insert attach;
        
        List<Attachment> attachments=[select id, name from Attachment where parent.id=:lstcase[0].id];
        System.assertEquals(1, attachments.size());
   
         CaseMergeEmail.casAttachment(DupCase, caseObj1.id);
          CaseMergeEmail.CasCommnet(DupCase, caseObj1.id);
      test.stopTest();
    }
}
Please help on this issue.
Andrew GAndrew G
In your original code, is there any criteria in the SELECT statement that may limit the returned list?   (I can't see from that screen shot)
It is most likely that query is returning no records, therefore your list.size() event does not equate to true.

regards
Andrew
Sebastian PageSebastian Page
Hi Andrew you are right i am shairng you  complte apex class code please review this code and help me 

Apex class is here-

public class CaseMergeEmail { 
    
    Public Static void casAttachment(List<String>dupCas ,String casId)
    {
        List<attachment> newAttachment =new list<attachment>();
        Map<String, attachment> MapAttach= new Map<String,attachment>();
        List<attachment> lstAttachments=[select id,Body,Name,ParentId from attachment where parentId in:dupCas];
        
        
        if(lstAttachments!=null && !lstAttachments.isEmpty())
        {
            for(attachment att:lstAttachments)  
            {      
                
                attachment newattch=new attachment();    
                newattch.parentId=casId;   
                newattch.Body=att.body;     
                newattch.name=att.name;  
                //system.debug(' newattch.Body'+ newattch.Body);
                //system.debug(' newattch.name'+ newattch.name);
                newAttachment.add(newattch);
                
            }
            insert newAttachment;
        }
    }
    Public Static void CasCommnet(List<String> dupCas,String casId){
        
        list<caseComment> NewCaseCommet=new List<caseComment>();
        list<caseComment> lstComment= [select id ,commentbody,parentid from casecomment where parentId in:dupCas];
        if(lstComment.size()>0)
        {
            for(casecomment cmt:lstComment)       
            {  caseComment newcmt=new casecomment(); 
             newcmt.ParentId=casId;         
             newcmt.CommentBody=cmt.CommentBody;  
             NewCaseCommet.add(newcmt);
            }
        }
        insert NewCaseCommet;
        
    }
    
    public Static void TicketLogs(List<case>dupCas)
    {
        List<Spam_Ticket_Log__c> bigListData=new list<Spam_Ticket_Log__c>();
        List<Case> CasLogLst=[select id ,Status,CaseNumber from Case Where Id In:dupCas];
        if(CasLogLst.size()>0)
        {
            for(case cs:CasLogLst)
            {
                
                Spam_Ticket_Log__c cm=new Spam_Ticket_Log__c();
                cm.Child_Ticket__c=cs.CaseNumber;
                
                bigListData.add(cm);
                
                
            } 
        }
        
        if(bigListData.size()>0)
        {
            
            insert bigListData;
        }
        
    }
    
}
Andrew GAndrew G
Hi Sebastian

I did respond to this over the weekend, but my first response seems to have disappeared.  So lets try again and see if i can recall what i wrote:

The issue is that the attachments will have nulls as their parent Id.  Why?  Because the Case records have not been inserted yet.  On insertion, we get the Id.  And the location in the code where you are populated the list of strings to hold the Ids, the values will be nulls.  Therefore, that list of strings is populated with Nulls and therefore the select statement will return no values.

Update your test class as below:

I have also added some tweaks such as looping the Case creation to reduce the number of repetitive lines.  I have moved the location of the Test.startTest() as it is better to have the actual code you are trying to test being isolated from the test data insertions.  I also stuck in some asserts to add some assurance to your code.
 
/**
 * Created by andrewg on 19/09/2020.
 */

@IsTest
public class CaseMergeHandlerTest {

    static testMethod void testMerge(){
        List<String>DupCase =new List<String>();
        Account testAccount = new Account(Name='Test Company Name123');
        insert testAccount;

        Contact con  = new Contact(LastName='Testcont' ,Email='sss@test.com', AccountId=testAccount.Id);
        insert con;

        List<Case>lstcase =new List<Case>();

        for(Integer i=0; i <2; i++){
            Case case = new Case(AccountId=testAccount.Id, ContactId = con.Id,Origin='Email');
            if(i==0) {case.Status='Action-Automated Escalation'; } else { case.Status ='Closed Duplicate'; }
        }
        insert lstcase;

        DupCase.add(lstcase[0].Id);
        DupCase.add(lstcase[1].Id);

        Attachment attach=new Attachment();
        attach.Name='test.html';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.Body=bodyBlob;
        attach.ParentId=lstcase[0].Id;
        insert attach;

        List<Attachment> attachments=[SELECT Id, Name, ParentId FROM Attachment WHERE Parent.Id=:lstcase[0].Id];
        System.assertEquals(1, attachments.size());  //Sanity check

        Test.startTest();

        CaseMergeEmail.casAttachment(DupCase, caseObj1.Id);
        CaseMergeEmail.CasCommnet(DupCase, caseObj1.Id);
        Test.stopTest();

        attachments=[SELECT Id, Name, ParentId FROM Attachment WHERE Parent.Id=:lstcase[0].Id];
        System.assertEquals(2, attachments.size());  //real check - did we duplicate the attachment
        System.assertEquals(attachments[0].Name, attachments[1].Name);

    }
}

Hope the above proves helpful

regards Andrew

p.s. All code provided as-is.
Sebastian PageSebastian Page
Thanks Andrew but i getting to many Soql query 101 error while running test class my test class here-
@IsTest
public class CaseMergeHandlerTest {

    static testMethod void testMerge(){
        List<String>DupCase =new List<String>();
        Account testAccount = new Account(Name='Test Company Name123');
        insert testAccount;

        Contact con  = new Contact(LastName='Testcont' ,Email='sss@test.com', AccountId=testAccount.Id);
        insert con;

        List<Case>lstcase =new List<Case>();

        for(Integer i=0; i <2; i++){
           lstcase.add(new Case(AccountId=testAccount.Id, ContactId = con.Id,Origin='Email'));
                    }
        insert lstcase;

        DupCase.add(lstcase[0].Id);
        DupCase.add(lstcase[1].Id);

        Attachment attach=new Attachment();
        attach.Name='test.html';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.Body=bodyBlob;
        attach.ParentId=lstcase[0].Id;
        insert attach;

        List<Attachment> attachments=[SELECT Id, Name, ParentId FROM Attachment WHERE Parent.Id=:lstcase[0].Id];
        System.assertEquals(1, attachments.size());  //Sanity check
        
       
                caseComment newcmt=new casecomment();
                newcmt.ParentId=lstcase[0].Id;
                newcmt.CommentBody='Test comments';
               insert newcmt;
        
        List<caseComment> lstCaseComments=[SELECT Id, CommentBody, ParentId FROM caseComment WHERE Parent.Id=:lstcase[0].Id limit 1];
        System.assertEquals(1, lstCaseComments.size());  //Sanity check

        Test.startTest();

        CaseMergeEmail.casAttachment(DupCase, lstcase[0].Id);
        CaseMergeEmail.CasCommnet(DupCase, lstcase[0].Id);
        CaseMergeEmail.TicketLogs(lstcase);
        Test.stopTest();

       attachments=[SELECT Id, Name, ParentId FROM Attachment WHERE Parent.Id=:lstcase[0].Id];
        System.assertEquals(2, attachments.size());  //real check - did we duplicate the attachment
       System.assertEquals(attachments[0].Name, attachments[1].Name);
       
        lstCaseComments=[SELECT Id, CommentBody,ParentId FROM CaseComment WHERE Parent.Id=:lstcase[0].Id];
         System.assertEquals(2, lstCaseComments.size()); 
        System.assertEquals(lstCaseComments[0].CommentBody, lstCaseComments[1].CommentBody);

    }
}