When you create a Barrel
, a Filling
is also created. Is there any chance to make a fake error in unit test between Barrel
and Filling
to crush the upsert process and cover the catch
block?
Helper:
public void insertFilling(List<Barrel__c> createdBarrels){
List<Filling__c> fillingsToInsert = new List<Filling__c>();
for(Barrel__c barrel : createdBarrels){
if(barrel.FinishDateTime__c >= Date.TODAY()){
fillingsToInsert.add(
new Filling__c(Barrel__c = barrel.Id, Barrel_ExternalID__c = barrel.Id)
);
}
}
if(!fillingsToInsert.isEmpty()){
try{
upsert fillingsToInsert Barrel_ExternalID__c;
//I want to cover this part:
} catch(Exception e){
//email error notification
}
}
}
Trigger:
trigger BarrelTrigger on Barrel__c (after insert) {
TriggerHelper helper = new TriggerHelper();
helper.insertFilling(Trigger.new);
}
Test:
@isTest static void testBarrelInsertException(){
Barrel__c barrel = new Barrel__c(
Name = 'TestBarrel',
Opportunity__c = null, //required field
FinishDateTime__c = DateTime.NOW().AddDays(1)
);
//But I even can't insert it. I'm getting the 'REQUIRED_FIELD_MISSING' error:
insert barrel;
//And still no coverage.
}