Test Class for AuraHandledException

Author: Bruce Tollefson Published: October 17, 2022; Modified: October 17, 2022
book

When creating test classes you want to make sure your goal of the test classes is not to just get the required apex code coverage. But instead to be testing the method / class / scenario. Which often times can result in wanting to make sure an error is presented.

There are multiple ways to test in a test class for an error it could be as simple as testing that you hit an error exception:

@isTest public static void accountInsertMethodTest(){ Test.startTest(); try{ ApexClass.InsertMethod(new Account(name='Test Insert Method')); }catch(Exception e){ Assert.isNotNull(e); } Test.stopTest(); }

The above test is only checking to see if an exception was thrown. It doesn’t specify the type of exception. If you know the exception that you are looking for it would be better to catch that exception. In the above if the account has another field required in order for it to be inserted a DmlException would be thrown. Catching that Exception specifically would help to make sure any additional errors thrown for other reasons that should make this test class fail does not inadvertently pass.

@isTest public static void accountInsertMethodTest(){ Test.startTest(); try{ ApexClass.InsertMethod(new Account(name='Test Insert Method')); }catch(DmlException e){ Assert.isNotNull(e); } Test.stopTest(); }

If this were a custom exception you could assert for the specific value of the message along with the exception.

What happens with the below code where you want to throw an error from an LWC lightning web component. Generally you use AuraHandledException. Example:

@AuraEnabled public static checkStringLength(String str){ if(str.length() < 5){ throw new AuraHandledException('The string must be at least 5 characters in length'); } }

The above code block would be used from an LWC to check that the length of the string is at least 5 characters if not throw an error that could be shown in the UI. If you tried to create a test class checking the message:

@isTest public static void checkStringLengthTest(){ Test.startTest(); try{ LWCApexClass.checkStringLength('abc'); }catch(AuraHandledException e){ Assert.areEqual('The string must be at least 5 characters in length', ae.getMessage()); } Test.stopTest(); }

This would create an error of ‘Script-Thrown’ even though this is still correct. In order to add the message for the test class to be able to view it the exception needs to set the message.

@AuraEnabled public static checkStringLength(String str){ if(str.length() < 5){ AuraHandledException ae = new AuraHandledException('The string must be at least 5 characters in length'); ae.setMessage('The string must be at least 5 characters in length'); // this could be different if you want the UI error message different than unit test messages throw ae; } }

Now the test class will catch the AuraHandledException.

Leave a Reply

Your email address will not be published. Required fields are marked *