I have a test method that is failing because it is expecting an error, but isn't getting one. Here's the test method:
static testMethod void assignToSelf_DmlError() {
setup();
Test.startTest();
delete contExt.kase;
contExt.sendNotificationEmail = true;
contExt.assignToSelf();
Integer numEmails = Limits.getEmailInvocations();
Test.stopTest();
System.debug(ApexPages.getMessages());
System.assert(ApexPages.hasMessages(ApexPages.Severity.ERROR), 'There should be an error message');
System.assertEquals(0, numEmails, 'No emails should be sent');
}
Essentially, this test is deleting a case, then calling the assignToSelf function. I'm expecting an error because assignToSelf attempts to update the case, as you can see below:
public void save() {
try {
Database.DmlOptions options = new Database.DmlOptions();
if(sendNotificationEmail) {
options.emailHeader.triggerUserEmail = true;
}
Database.update(kase, options);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.confirm, CASE_OWNER_CHANGED_SUCCESSFULLY));
}
catch(DMLException dmx) {
ApexPages.addMessages(dmx);
return;
}
}
public void assignToSelf() {
kase.OwnerId = UserInfo.getUserId();
save();
}
Why am I not catching the exception I'm expecting?
Severity
should beFATAL
?