function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew EAndrew E 

Testing AuraHandledException in Test Class

Hi all,

I have some apex code that looks like the code below. In my test class, I have a testMethod for successful execution, but I'm having trouble creating a testMethod for the exception. I've been able to trigger the exception, but the test is marked as Fail and I recieve "System.AuraHandledException: Script-thrown exception". As a result, I'm not getting code coverage for this exception.

How can I successfully test an AuraHandledException from a testMethod without breaking the test and having it result in a Fail?
try{
   update account;
}
catch(Exception e){
   throw new AuraHandledException('error updating account');
}

 
Best Answer chosen by Andrew E
Nayana KNayana K
In your test method also you have to add try catch block. Suppose if you set account.name blank they you know it will throw exception :

account.name  = '';

try
{
 //call the above method which updates the account
}
catch(Exception e) 
{

}
 

All Answers

Nayana KNayana K
In your test method also you have to add try catch block. Suppose if you set account.name blank they you know it will throw exception :

account.name  = '';

try
{
 //call the above method which updates the account
}
catch(Exception e) 
{

}
 
This was selected as the best answer
katta srini 6katta srini 6
Thank you @Nayana K.
It resolved my issue with a try-catch block in my test class.
All Finance DealsAll Finance Deals
Allfinancedeals.com is a website that always believes in reality and authentic information about all types of financial activities. We are committed to providing you with all types of creative financial solutions that will need for yourself, your family, and your friends.

Learn More

https://allfinancedeals.com/online-banks-that-use-zelle.html
https://allfinancedeals.com/link-bank-account-to-cash-app-without-debit-card.html
https://allfinancedeals.com/can-you-add-money-to-venmo-card-at-atm.html
https://allfinancedeals.com/pay-for-pizza-with-routing-number.html
https://allfinancedeals.com/how-to-shop-online-with-a-stolen-credit-card.html
https://allfinancedeals.com/gift-card-sites-that-accept-paypal.html
https://allfinancedeals.com/how-to-use-google-pay-at-atm.html
https://allfinancedeals.com/how-to-use-virtual-visa-card-in-store.html
https://allfinancedeals.com/pay-for-food-with-bank-account-number.html
Allfinancedeals.com is a website that always believes in reality and authentic information about all types of financial activities. We are committed to providing you with all types of creative financial solutions that will need for yourself, your family, and your friends.
cekoge hreancekoge hrean
MyChickfilaVisit Survey is an online feedback program designed to get honest feedback, reviews, and suggestions from your recent experience. MyChickfilaVisit Survey