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
Girbson Bijou 7Girbson Bijou 7 

Increase coverage

Help me in increasing  the coverage of a class and writing  test class for a tiggger which refer a class.
The class is: 
public class DetailedDistributionTrigger
{
    public static void afterUpdateOperation(List<Container__c> lstNewContainers, Map<Id, Container__c> mapOldContainers)
    {
        List<String> lstUpdatedContainerIds = new List<String>();
        
        for(Container__c newContainer : lstNewContainers)
        {
            Container__c oldContainer = new Container__c();
            
            oldContainer = mapOldContainers.get(newContainer.Id);
            
            if(newContainer.Percent_Distributed__c == 100 && oldContainer.Percent_Distributed__c != 100)
            {
                lstUpdatedContainerIds.add(newContainer.Id);
            }
        }
        
        if(!lstUpdatedContainerIds.isEmpty())
            sendContainerPDF(String.join(lstUpdatedContainerIds,','));
    }
    
    @future(callout = true)
    public static void sendContainerPDF(String strUpdatedContainers)
    {
        List<String> lstContainerIds = new List<String>();
        lstContainerIds = strUpdatedContainers.split(',');
        
        List<Container__c> lstContainers = new List<Container__c>();
        
        if(!lstContainerIds.isEmpty())
        {
            lstContainers = [SELECT Id,Name, Owner.Email FROM Container__c WHERE Id IN :lstContainerIds];
        }
        
        for(Container__c Container :lstContainers)
        {
           // if(Container.Requisitor__c != null && Container.Requisitor__r.Contact_email__c != null)
           // {
            //    sendpdfEmail(Container,Container.Requisitor__r.Contact_email__c);
          //  }
            
          if(Container.Owner.Email != null)
           {
              sendpdfEmail(Container, Container.Owner.Email);
           }
        }  
    }
    
    public static void sendpdfEmail(Container__c Container, String emailid)
    {
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

        // Reference the attachment page and pass in the account ID
        PageReference pdf =  Page.Container_Report;
        pdf.getParameters().put('id',Container.Id); 
        pdf.setRedirect(true);
        Blob pdfData;
        
        // Take the PDF content
        if(!Test.isRunningTest())
        {
            pdfData = pdf.getContent();
        }
        else
        {
            pdfData = Blob.valueOf('test');
        }
        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(Container.Name + '.pdf');
        efa.setBody(pdfData);

        //String[] toAddresses = new List<String> {emailId};
        //email addresses to send email
          String[] toAddresses = new List<String>{'bijou.girbs@gmail.com'};

        // Sets the paramaters of the email
        email.setSubject(Container.Name +' Detailed Distribution Report');
        email.setToAddresses( toAddresses );
        email.setPlainTextBody('The Container ' + Container.Name +  ' has been totally distributed, Please find attached the detailed distribution report' );

        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

        // Sends the email
        Messaging.SendEmailResult [] r = 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}



The Test Class is:

@IsTest
private class DetailedDistributionTriggerTest{
static   TestMethod void  DetailedDistributionTrigger(){
Container__c cont = new Container__c ( Name = 'CMLU', Provenance__c='OTHER', Statut__c='Open');
          insert cont;
    
     Articles_Containers__c ac = new Articles_Containers__c();
          Product2 p = new Product2(Name ='TestProduct'); 
          insert p;
    
                 
          ac.Product__c=p.ID;
          ac.Unit_Weight__c = 45; 
          ac.Unit_Cost__c = 87;
          ac.Container__c= cont.ID;
          ac.Number__c = 55;
          ac.UM__c ='UNIT(S)';    
          ac.Local_ID__c = 7888;
          ac.Comments__c = 'UNIT(S)';
          ac.Purpose__c='Consignment';
          ac.Condition__c= 'New';
           
           insert ac;
    Account acc = new Account(Name = 'TestACC', Representant__c='TestBene', Departement__c='Ouest', Address__c='102, Test');
    insert acc;
    
    Delivery__c del = new Delivery__c();
        del.Beneficiaire__c = acc.Id;
        del.Ration__c =3;
        del.Delivery_status__c='Pending';
        
        insert del;
    
    Item_Distributed__c itemDis = new Item_Distributed__c();
    itemDis.Quantity__c = 44;
    itemDis.Product__c = ac.Id;
    itemDis.Delivery__c =del.Id;
    
    insert itemDis;
    
    Test.setCurrentPageReference(new PageReference('Page.Container__Report')); 
    System.currentPageReference().getParameters().put('id', cont.ID);
    DetailedDistributionTrigger ddt = new DetailedDistributionTrigger();

}
}



The Trigger is :

trigger DetailedDistrib on Container__c (After Update)
{
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        DetailedDistributionTrigger.afterUpdateOperation(Trigger.New, Trigger.oldMap);
    }
}

 
Om PrakashOm Prakash
Apex class DetailedDistributionTrigger is called from Trigger, so in test method you need to update the Container__c object.
Modify your test class according to bellow and then try execution. Let me know if any other issue in the same.
 
// Bellow lines are commented which is no longer required
/* Test.setCurrentPageReference(new PageReference('Page.Container__Report')); 
   System.currentPageReference().getParameters().put('id', cont.ID);
    DetailedDistributionTrigger ddt = new DetailedDistributionTrigger();*/
// Update the Container__c record
   cont.Percent_Distributed__c = 100;
   update cont;