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: 
AccSt = [Select Id,Account_Status__c from Account WHERE job__c in :patientIds ];
  for(Account_Status__c a : AccSt)
    a.Account_Status__c = true;
   update updatedAccSt;
      // here is code to send Email and other operation
below is the test class.
public class SetAccountStatustest {
    static testMethod void test()
	patient__c pat = new patient__c();
		//some field
		insert pat;
	  Account acc = new Account();
	  //some field;
	  insert acc;
	  job__c acc = new job__c();
	  //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;
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.