Intentionally fail apex test when expected exception is not thrown

Telegram logo Join our Telegram Channel

Hello there, imagine you have a code block that might throw an exception, and you want to test a scenario when the exception is excepted to be thrown. The new Assert.fail() function is very useful for such situations.

Basically, you can fail the test method if the expected exception is not thrown by the code you are testing. The test method will be passed if the expected exception is thrown. I have created a simple class to demonstrate this.


Code to be tested

public class ContactUpdateService {    
    public static void insertContacts(List<Contact> contacts){
        insert contacts;
    }
}

Please note that this is a very oversimplified scenario, we know, that a contact record will not be inserted if any of the required fields are not provided. For simplicity, I am using the standard LastName field, in real-time it can be any field.


Test scenario which throws an exception: Test Passes

The below code will throw an exception because LastName is not set to the contact that is being inserted. This will result in DMLException.

This test method will not fail because the exception is thrown. Also, we are checking if the thrown exception is DMLException just to make sure that the exception is thrown for a DML error and not any other problem. If any other exception is thrown, our test class will not pass.

The Assert.isInstanceOfType function is used to assert if the exception thrown is the same as our expectation. If it is not, then the test will result in failure.

@isTest
static void testInsert(){
    try {
        ContactUpdateService.insertContacts(new List<Contact>{ new Contact(FirstName = 'Rahul') });
        Assert.fail();
    } catch (Exception e){
        System.debug('Exception is excepted to be thrown ' + (e instanceof DMLException));
        Assert.isInstanceOfType(e, DMLException.class);
    }
}


Test scenario which throws an exception: Test fails

The below code will not throw an exception (because we are providing all required fields for contact to insert) which is why the test case will not be passed.

This type of test case we can use when we want our code not to perform some operation, and if the developer tries to perform that unknowingly or the code changes in an unintended way then the test case will fail.

The below test case alerts the developer that, there is some change that prevented the code from throwing an exception when it must throw an exception. 

@isTest
static void testInsert2(){
    try {
        ContactUpdateService.insertContacts(new List<Contact>{ new Contact(LastName = 'Gawale') });
        Assert.fail();
    } catch (Exception e){
        System.debug('Exception is excepted to be thrown ' + (e instanceof DMLException));
        Assert.isInstanceOfType(e, DMLException.class);
    }
}


Hope this was helpful. Please comment down below if you want more such posts about testing.

No comments :
Post a Comment

Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, email us at rahul@forcetrails.com