You need to sign in to do that
Don't have an account?
Kristiana Granger
How to cover catch block for the test class?
Hi, All
I am new to salesforce, can anyone help me to get 100% coverage please
public class MyContactListController
{
@AuraEnabled
public static List<Contact> getContacts(Id recordId)
{
List<Contact> conList = new List<Contact>();
try{
System.debug('--'+recordId);
conList = [Select Id, FirstName, LastName, Email, Phone From Contact Where AccountId = :recordId];
System.debug('conlist result----> '+conList);
}
catch(Exception e){
System.debug('inside catch Block -> '+e.getMessage());
}
return conList;
}
}
-----------------------------------------
TestClass
-----------------------------------------
@isTest
public class MyContactListControllerTest {
static testMethod void getContactsTest(){
Account ac = new Account(Name='Name1');
Insert ac;
List<Account> acList = [select id, name from Account where id!=NULL];
System.assertEquals(true,acList.size()>0);
MyContactListController.getContacts(acList[0].id);
List<Case> case_Obj = [SELECT Id,CaseNumber,AccountId From Case WHERE AccountId=:acList[0].Id LIMIT 10];
MyContactListController.getContacts(acList[0].Id);
}
}
I am new to salesforce, can anyone help me to get 100% coverage please
public class MyContactListController
{
@AuraEnabled
public static List<Contact> getContacts(Id recordId)
{
List<Contact> conList = new List<Contact>();
try{
System.debug('--'+recordId);
conList = [Select Id, FirstName, LastName, Email, Phone From Contact Where AccountId = :recordId];
System.debug('conlist result----> '+conList);
}
catch(Exception e){
System.debug('inside catch Block -> '+e.getMessage());
}
return conList;
}
}
-----------------------------------------
TestClass
-----------------------------------------
@isTest
public class MyContactListControllerTest {
static testMethod void getContactsTest(){
Account ac = new Account(Name='Name1');
Insert ac;
List<Account> acList = [select id, name from Account where id!=NULL];
System.assertEquals(true,acList.size()>0);
MyContactListController.getContacts(acList[0].id);
List<Case> case_Obj = [SELECT Id,CaseNumber,AccountId From Case WHERE AccountId=:acList[0].Id LIMIT 10];
MyContactListController.getContacts(acList[0].Id);
}
}
You can cover catch by not giving the required field that is needed when some operation is performing in Apex class. Test class will cover catch block whenever the error will occur in the main class. Like this:-
This will cover catch as there is no contact with the account.
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
Try Below Code
Made Some Changes Which Are In Bold Please Mark It As Best Answer If It Helps
Thank You!
@Suraj Tripathi 47 - Thank you for Quick reply, Can you please suggest other way as with given code not able to achieve 100%
@CharuDutt - Thank you for Quick and detailed reply, I see when I try to save given code (in bold) it wont allow me to save, but yea I am exploring that method, its good to know the new method
Can you please Tell why It's Not Allowing To Save
its giving below error -
"Method does not exist or incorrect signature: void isRunningTest() from the type Test"
It's Because May be There's A Class Named test In Yours ORG That's Why It's Not Letting You Save The Class
I There's Another Class With The Name Test Change That Class Name
You can try suggestion posted in below link
https://salesforce.stackexchange.com/questions/57344/how-to-cover-catch-statement-in-test-class
Thanks!
Please use below code for 100% code coverage without changes in your main class
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
https://lulubox.top/
//Main Class
public class AccountHandler{
public static void insertAccount(Account acc){
try{
insert acc;
}catch(DMLException dex){
system.debug('dml exception occured: ' + dex.getMessage());
}
}
}
//Test Class
@IsTest
public class AccountHandlerTest{
public static testMethod void insertAccountTest(){
//Cover try block
Account acc = new Account(Name = 'Test Account');
AccountHandler.insertAccount(acc);
//Cover catch block
Account accnt = new Account(); //drop required field to throw exception deliberately
//catch exception you are getting in test class as shown below
try{
AccountHandler.insertAccount(accnt);
}catch(DMLException dex){
System.debug ('all right' + dex.getMessage());
return;
}
System.assert(false, 'a dml exception was expected but was not thrown');
}
}