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() {

        delete contExt.kase;
        contExt.sendNotificationEmail = true;
        Integer numEmails = Limits.getEmailInvocations();

    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) {

public void assignToSelf() {
    kase.OwnerId = UserInfo.getUserId();

Why am I not catching the exception I'm expecting?

  • 1
    Perhaps the Severity should be FATAL? – Adrian Larson Feb 8 '16 at 19:56
  • I was thinking I had the Severity wrong, but my debug statement is actually returning "USER_DEBUG [79]|DEBUG|(ApexPages.Message["The case owner was changed successfully"])" I'm wasn't expecting that it would return that message since the method should have already failed. – Charles Naughton Feb 8 '16 at 19:58
  • does the delete actually succeed in the debug logs? – cropredy Feb 9 '16 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.