I've a Apex class as below and i've to write test class for that.
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(DMLException e){
// here is code to send Email and other operation
}
}
}
If this class may has mistake please consider, because this is not a actual class this is just example to explain my senario. My class is too big.
I'm writing test class and i've to cover the catch block in test class. but to cover the catch we need to pass such data which we cause run time exception i.e. DML exception but this is not possible in my case; to occur run time exception.
So to cover try block i'm thinking throw the exception manually so that it will cover the catch block.
public class myexceptionextends Exception {}
// method start
try{
update updatedAccSt;
if(Test.isRunningTest()){
throw new myexception('Error');
// it goes to catch block
}
}
SO here problem is exception must be DML exception it gives error Non-virtual and non-abstract type cannot be extended: