0

I am working on the below test class to cover trigger helper class . Test class code is below :

@isTest(SeeAllData=false)
public class AttachmentTriggerTest {
    @testSetup
    public static void createTestData() {  
        //creating Custom Setting record
        TestUtility.insertCustomSetting();

        Task tsk = TestUtility.createTask(); 
        insert tsk;   
        List<Attachment> attch = TestUtility.createAttachmentForTaskBulkified(tsk,5);
        insert attch;

        Event evt = TestUtility.createEvent(); 
        insert evt;           

        List<Attachment> attches = TestUtility.createAttachmentForEventBulkified(evt,5);
        insert attches;
    }
    static testMethod void DealerEventTestMethod() 
    {
        Test.startTest();
        List<Attachment> attachments=[select id,parentid  from Attachment limit 20];
        system.debug('attachments parentid'+attachments);
        Delete attachments;

        Test.stopTest();
    }


}

Below is the class : ***********************************************************************************************/

public with sharing class 

AttachmentTriggerHelper{

    //getting the User ID of the loggedin User
    String LoggedinUserId = Userinfo.getUserId();
    BypassTriggerLogicSettings__c customSetting = BypassTriggerLogicSettings__c.getInstance(LoggedinUserId);

    //Method to check all the related attachment and task Ids related to current attachment in the context    
    public void checkTaskIDRelatedToAttachment(list<Attachment> AttList){       
        String sobjType=null;
        //Using set and list object for DML operation        
        List<Task> tsk = new List<Task>();
        List<Event> evt = new List<Event>();
        Set<Id> tskIds = new Set<Id>();

        try{
            //checking the custom setting for the trigger
            if(customSetting.Bypassed_Trigger_Methods__c == null || !customSetting.Bypassed_Trigger_Methods__c.contains('checkTaskIDRelatedToAttachment')){
                {
                    // For each attachment checking whether they are the event type or task type        
                    for(Attachment attach : AttList){
                        if(attach.ParentId.getSobjectType() == Task.SobjectType){  
                            tskIds.add(attach.ParentId);
                            sobjType='task';
                        }else if(attach.ParentId.getSobjectType() == Event.SobjectType){   
                            tskIds.add(attach.ParentId);
                            sobjType='event';
                        }
                    }
                    //If the CRUD is on the task object then updating the field on the task object
                    system.debug('sobjType values'+sobjType);
                    if(sobjType=='task'){
                        **List<Task> tskIn = new List<Task>();
                        if(tskIds!=null)
                            tskIn =([select id from task where id in :tskIds limit 50000]);
                        if(tskIn != null && !tskIn.isEmpty()){
                            for(Task tIn : tskIn )
                            {
                                tIn.Attachment__c=false;
                                tsk.add(tIn);
                            }
                        }
                        update tsk;**
                    }else{
                        //If the CRUD is on the event object then updating the field on the event object
                        List<Event> evtIn = new List<Event>();
                        evtIn =([select id from event where id in :tskIds limit 50000]);
                        if(evtIn != null && !evtIn.isEmpty())
                        {
                            for(Event eIn : evtIn )
                            {
                                eIn.Attachment__c=false;
                                evt.add(eIn);
                            }
                        }
                        update evt;
                    }
                }  
            }
        }
        **catch (Exception err){
            Error_Log__c errLog = new Error_Log__c(Log_Name__c = 'EventTriggerHelper.OnEventDeleteController', Log_Message__c = 'SFDC__LOG: '+err.getStackTraceString()+' Line: '+err.getLineNumber(), Log_Type__c = 'Trigger Helper' , Log_Severity__c = 'HIGH'); 
            if(errLog!=null)
                insert errLog;**
        } 
    }
}

 public static Task createTask(){
        Task tsk = new Task();
        tsk.ownerid=userinfo.getuserId();
        tsk.Description='Testing Data';
        return tsk;

    }

// Set up method for attachment creation - Bulkified

   public static List<Attachment> createAttachmentForTaskBulkified(Task tsk,integer count){
         List<Attachment> attBul = new list<Attachment >();

        for(Integer i = 0 ; i<=count ;i++)
            {
          Attachment attach=new Attachment();     
        attach.Name='Unit Test Attachment'+i;
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=tsk.id;         
         attBul.add(attach);        
               }
       return attBul;
   }

Lines between ** I am not able to cover. I can handle the catch , But the main concern is the code uncovered. I am using common utility class to create the test data.

  • It seems that you are not assigning the parent id while creating the attachment in your test utility class.Make sure you assign the task id to parentId of attachment in your test data – Samir Mar 9 '17 at 15:11
  • can you also show the code for createTask and createAttachmentForTaskBulkified methods? – Novarg Mar 9 '17 at 15:32
  • Have you queried back for Error_Log__c records? – Adrian Larson Mar 9 '17 at 15:40
  • Removing all your code after getting answers is abusive behavior. Please don't do that. – Adrian Larson Apr 11 '17 at 21:56
  • @cloudy-ritesh Vandalizing your own posts is not being a good citizen. Please do not make a habit of this behavior. It is an abuse of the community and will not be tolerated if you make it a frequent occurrence. Note that even if you remove the code from your post, it is still visible in the edit history and only a moderator can redact that edit history. If you want your code to be private, do not publish it on the internet in the first place. – Adrian Larson Apr 11 '17 at 22:54
2

Here's your issue

In this snippet:

for(Attachment attach : AttList){
                        if(attach.ParentId.getSobjectType() == Task.SobjectType){  
                            tskIds.add(attach.ParentId);
                            sobjType='task';
                        }else if(attach.ParentId.getSobjectType() == Event.SobjectType){   
                            tskIds.add(attach.ParentId);
                            sobjType='event';
                        }
                    }

you assign to a variable outside of the loop (i.e. sobjType) based on the last Attachment processed in the list. Since your test code mocks Attachments on both Tasks and Events, then at run time, the last Attachment fetched by SOQL in your test method that is passed to the delete DML operation happens to be an Event attachment. Hence, when you drop down to this line:

if(sobjType=='task'){..

sobjType is event !

You'll need to rework your logic so you save up the triggered attachments into two lists or maps and then process each list/map separately without using an if-then-else

  • 1
    And add some asserts – Eric Mar 11 '17 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.