1

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?

  • 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.