Forked from michalvavra/AuraHandledExceptionTest.cls
Created
September 2, 2024 16:19
-
-
Save apariciodevsf/2cc81da38d38c9eb9d106714e2737326 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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