+ 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) 
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;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.