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
Vasundhara Velgoti 4Vasundhara Velgoti 4 

I am trying to get the coverage for the catch block as it is throwing error. Can anyone help me out

Hi ,

Below is the trigger :
trigger POTriggerUpdate on PurchaseOrder__c (after insert, after update) {
    
     //Creating Project Plan in PurchaseOrder__c
    
     if(((trigger.isUpdate) && trigger.isAfter) || test.isRunningTest())
    {
       
    try
     {
      
           POTriggerLogic.afterInsertAfterUpdateLogic(trigger.new,trigger.oldMap,true);  
      }
      catch(DMLException e)
      {
         
      trigger.new[0].addError('Updation not possible because '+ e.getMessage());
          
     }
        
    }
    //End of Update Project plan in PurchaseOrder__c 

}

Test class:
@isTest
public class Test_POTriggerUpdate {
    
     @isTest static void unittest(){
        Test.startTest();
        try
        {
           
          Krow__Project__c kp = new Krow__Project__c();
          kp.Id=kp.Id;
       
        insert kp;
        Krow__Task__c kt = new Krow__Task__c();
        kt.Id=kt.Id;
        kt.Name='test';
        kt.Krow__Start_Date__c = Date.today();
        kt.Krow__Due_Date__c   = Date.today();
        kt.Krow__Project__c    = kp.Id;
        
        kt.Krow__Parent_Task__c= null;
        kt.Actual_Due_Date__c  = null;
        kt.Krow__Project_Phase__c=false;
        kt.Create_Task_Assignment__c=true;
        insert kt;
           PurchaseOrder__c po                                   =  new PurchaseOrder__c();
                          
                          po.Name                              = 'Test order'; 
                           po.Status__c                         = 'Draft';
                          po.Engagement_Plan_Create__c         = false;
                          po.Engagement_Plan_Template__c       = kp.Id;
                          po.Engagement_Plan_Update__c         = false;
                          po.Engagement_Plan_Update_Template__c= kp.Id;
                         // po.Engagement_Plan__c                = opp.Engagement_Plan__c;
                   insert po;
                          po.Name                              = 'Test order1';
                         
                          po.Status__c                         = 'GM Approval Pending';
                          po.Engagement_Plan_Create__c         =  true;
                          po.Engagement_Plan_Update__c         = true;
                          po.Engagement_Plan_Template__c       = kp.Id;
                          po.Engagement_Plan_Update_Template__c= kp.Id;
                          //po.Engagement_Plan__c                = null;
                 

       
       
        }
        catch(Exception e) {
            Boolean expectedError=  e.getMessage().contains('Updation not possible because') ? true : false;
         System.AssertEquals(expectedError, true); 
        }
        Test.stopTest();
             
        /* Opportunity opp                                       =  new Opportunity();
                    opp.Name                                   = 'Test Opp';
                    opp.StageName                              = 'Target'; 
                    opp.Engagement_Plan_Create__c              =  false;
                    opp.Engagement_Plan_Template__c            =  kp.Id;
                    opp.Engagement_Plan__c                     =  kp.Id;
                    opp.CloseDate                              =  Date.today();  
             insert opp;*/
               
        

        
    }

}

 
Nithesh NNithesh N
Hi Vasundhara,
Please refer the following links that helps you throw DML Exception manually in test method to increase your Test Coverage.
Link 1 (https://salesforce.stackexchange.com/questions/130759/how-to-throw-dml-exception-manually)
Link 2 (https://salesforce.stackexchange.com/questions/101111/how-to-cause-dmlexception-delete)

 
GulshanRajGulshanRaj
Hi Vashundhara,

You can throw exception inside your wrapper class function POTriggerLogic.afterInsertAfterUpdateLogic:
if(Test.isRunningTest() && some other condition)
{
   throw new DMLException(); 
}

There are several ways to invalidate DML operation. You can use any one of such techniques.


If this helps you, mark this question as solved and choose best answer so it will help other in future to judge correct solution.


Thanks
Gulshan Raj