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
Amol ChAmol Ch 

how to cover Catch block in test class

how to cover catch block in test class. below is the trigger. because if controller goes to update updatedAccSt  then it always update account
trigger SetAccountStatus on job__c (after insert) {
List<Account> AccSt = new List<Account_Status__c>();
List<Account> updatedAccSt = new List<Account_Status__c>();
List<Id> patientIds = new List<Id>();
for (job__c  job: trigger.new) 
	{
     patientIds.add(patient.id);
	}
	
AccSt = [Select Id,Account_Status__c from Account WHERE job__c in :patientIds ];
 
  for(Account_Status__c a : AccSt)
  {
    a.Account_Status__c = true;
    updatedAccSt.add(a);
  }
 
  if(updatedAccSt.size()>0)
  {
  try{
   update updatedAccSt;
   }
   catch{
      // here is code to send Email and other operation
   
   }
  }
}
below is the test class.
@istest(seealldata=false)
public class SetAccountStatustest {
    static testMethod void test()
    {
	patient__c pat = new patient__c();
		pat.name__c=Test;
		//some field
		insert pat;
		
	  Account acc = new Account();
	  acc.Name=Test;
	  acc.Patient__C=pat.id;
	  acc.Account_Status__c
	  //some field;
	  insert acc;
	  
	  job__c acc = new job__c();
	  job.Name__c=test;
	  //some field
	  insert job;
	  
	 }
	}
The above class is only example. my code is big code due to this i put here example.

 
Naveen Nelavelli 9Naveen Nelavelli 9
Hi Amol ,basically you need to pass testd ata to create run time exception.while passing test data 
empty value of required field for any record in updatedAccSt  list or pass test data in such a way that it will break validation.

Let me know if you dont have any validation rule and required fields.
Amol ChAmol Ch
Hi Naveen,

I've validation and required field also, but in my code conditon arrise such that we have used  field of patient and account in actual class . so we need to insert account and patient record correct without fails. actual senario such that first we have to insert record successfully and then make the condtion such that it fails during updation of same record  then and only then catch block wiht execute.
Amol ChAmol Ch
you can see apex class code here;http://salesforce.stackexchange.com/questions/126137/test-class-not-showing-code-coverage
Amol ChAmol Ch
Hi Manish,

That class has same trigger before insert before update event. my test class also too big, not able to put here.