Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save apariciodevsf/2cc81da38d38c9eb9d106714e2737326 to your computer and use it in GitHub Desktop.

Select an option

Save apariciodevsf/2cc81da38d38c9eb9d106714e2737326 to your computer and use it in GitHub Desktop.
@AuraEnabled
static void throwAnException() {
AuraHandledException auraException = new AuraHandledException('The message that displays in Lightning');
auraException.setMessage('The message that you can get in a test');
// If setMessage() is not used, the exception messege won't be present in console (deploys, unit tests, ...)
throw auraException;
}
@IsTest
static void testThrowAnException() {
Exception capturedException;
try {
throwAnException();
} catch (Exception e) {
capturedException = e;
}
System.assertNotEquals(null, capturedException, 'Exception should be thrown');
System.assertEquals(AuraHandledException.class.getName(), capturedException.getTypeName(), 'AuraHandledException is expected');
System.assertEquals('The message that you can get in a test', capturedException.getMessage(), 'Specific error message is expected');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment