You need to sign in to do that
Don't have an account?
Goswami Puri
How to cover return statement in catch block in test class
Hi ,
I am getting issue in catch block due to retrun statement .Can anyone help me in this
Main Class
public with sharing class Common_DAO
{
//get the list of Table Component records
public static List<Common_TableComponent__c> getTableComponentList(string recordName)
{
try
{
List<Common_TableComponent__c> tempList = new List<Common_TableComponent__c>();
tempList = [SELECT ActionButtons__c,Advanced_Search_Criteria__c,Advanced_Search_Help_Text__c,
ColumnHeaderList__c,Columns_To_Sort__c,
Date_Filter_Values_New__c,DefaultView__c,Default_Filter_Column__c,Enable_Advanced_Search__c,Enable_Date_Filter__c,
Enable_Search__c,Enable_Sorting__c,Fields__c,IsActionColumnEnabled__c,Order_By_Clause__c,
Pagination_Type__c,SearchFields__c,Search_Help_Text__c,SOQLQuery__c,Where_Clause__c, Header_Default_Value__c,Header_Spanish_Value__c
FROM Common_TableComponent__c WHERE Name = :recordName LIMIT 1];
return tempList;
}
catch(exception e)
{
return null;
}
}
//get the list of Table Search records
public static List<Common_TableSearch__c> getTableSearchList(string recordName, string searchType)
{
try
{
List<Common_TableSearch__c> tempList = new List<Common_TableSearch__c>();
tempList = [SELECT Name, Advanced_Search_SOQL_Query__c, Common_TableComponent__c,
Field_Type__c, Join_Column__c, Placeholder_Text__c, Possible_Values__c,
Search_Type__c, Child_Column__c,Default_Filter_Column__c,Where__c,Order_By__c,Default_Filter_Value_SOQL__c,Default_Filter_Child_Column__c
from Common_TableSearch__c
where Common_TableComponent__r.Name = :recordName and Search_Type__c=: searchType order by Sequence_Number__c ];
return tempList;
}
catch(exception e)
{
return null;
}
}
//get the list of Table Search records
public static List<Common_TableAction__c> getTableActionList(set<string> buttonsNameSet)
{
try
{
List<Common_TableAction__c> tempList = new List<Common_TableAction__c>();
tempList = [SELECT Name,Action_Button_Display_Column_Values__c,
Action_Button_Display_Criteria_Column__c,
Action_Button_Ext_Display_Column_Values__c,
Action_Button_External_Display_Criteria__c,
APINameOfLink__c,Custom_Permissions__c,DynamicParams__c,
EnableAsLink__c,Is_Absolute_URL__c,IsActive__c,
Is_Remote_Action_Button__c,Enable_Pop_up__c,Self_Window__c,
Remote_Action_Class__c,Remote_Action_Method__c,Title__c,URL__c
FROM Common_TableAction__c where Name IN : buttonsNameSet];
return tempList;
}
catch(exception e)
{
return null;
}
}
}
Test class
@IsTest
Public class Common_DAO_Test{
@isTest static testmethod void common_DAOTest1(){
string recordName='';
string searchType='';
set<string> buttonsNameSet=new set<string> ();
Common_DAO c=new Common_DAO();
Common_DAO.getTableComponentList(recordName);
Common_DAO.getTableSearchList(recordName,searchType);
Common_DAO.getTableActionList(buttonsNameSet);
system.assertequals(searchType,'');
}
Public static testmethod void common_DAOTest2(){
Common_DAO c=new Common_DAO();
string recordName='test';
string searchType='test';
set<string> buttonsNameSet=new set<string> {'test'};
try{
Common_DAO.getTableComponentList(recordName);
} catch(DMLException e) {
system.assertEquals(e.getMessage(), e.getMessage());
}
Common_DAO.getTableSearchList(recordName,searchType);
Common_DAO.getTableActionList(buttonsNameSet);
}
I am getting issue in catch block due to retrun statement .Can anyone help me in this
Main Class
public with sharing class Common_DAO
{
//get the list of Table Component records
public static List<Common_TableComponent__c> getTableComponentList(string recordName)
{
try
{
List<Common_TableComponent__c> tempList = new List<Common_TableComponent__c>();
tempList = [SELECT ActionButtons__c,Advanced_Search_Criteria__c,Advanced_Search_Help_Text__c,
ColumnHeaderList__c,Columns_To_Sort__c,
Date_Filter_Values_New__c,DefaultView__c,Default_Filter_Column__c,Enable_Advanced_Search__c,Enable_Date_Filter__c,
Enable_Search__c,Enable_Sorting__c,Fields__c,IsActionColumnEnabled__c,Order_By_Clause__c,
Pagination_Type__c,SearchFields__c,Search_Help_Text__c,SOQLQuery__c,Where_Clause__c, Header_Default_Value__c,Header_Spanish_Value__c
FROM Common_TableComponent__c WHERE Name = :recordName LIMIT 1];
return tempList;
}
catch(exception e)
{
return null;
}
}
//get the list of Table Search records
public static List<Common_TableSearch__c> getTableSearchList(string recordName, string searchType)
{
try
{
List<Common_TableSearch__c> tempList = new List<Common_TableSearch__c>();
tempList = [SELECT Name, Advanced_Search_SOQL_Query__c, Common_TableComponent__c,
Field_Type__c, Join_Column__c, Placeholder_Text__c, Possible_Values__c,
Search_Type__c, Child_Column__c,Default_Filter_Column__c,Where__c,Order_By__c,Default_Filter_Value_SOQL__c,Default_Filter_Child_Column__c
from Common_TableSearch__c
where Common_TableComponent__r.Name = :recordName and Search_Type__c=: searchType order by Sequence_Number__c ];
return tempList;
}
catch(exception e)
{
return null;
}
}
//get the list of Table Search records
public static List<Common_TableAction__c> getTableActionList(set<string> buttonsNameSet)
{
try
{
List<Common_TableAction__c> tempList = new List<Common_TableAction__c>();
tempList = [SELECT Name,Action_Button_Display_Column_Values__c,
Action_Button_Display_Criteria_Column__c,
Action_Button_Ext_Display_Column_Values__c,
Action_Button_External_Display_Criteria__c,
APINameOfLink__c,Custom_Permissions__c,DynamicParams__c,
EnableAsLink__c,Is_Absolute_URL__c,IsActive__c,
Is_Remote_Action_Button__c,Enable_Pop_up__c,Self_Window__c,
Remote_Action_Class__c,Remote_Action_Method__c,Title__c,URL__c
FROM Common_TableAction__c where Name IN : buttonsNameSet];
return tempList;
}
catch(exception e)
{
return null;
}
}
}
Test class
@IsTest
Public class Common_DAO_Test{
@isTest static testmethod void common_DAOTest1(){
string recordName='';
string searchType='';
set<string> buttonsNameSet=new set<string> ();
Common_DAO c=new Common_DAO();
Common_DAO.getTableComponentList(recordName);
Common_DAO.getTableSearchList(recordName,searchType);
Common_DAO.getTableActionList(buttonsNameSet);
system.assertequals(searchType,'');
}
Public static testmethod void common_DAOTest2(){
Common_DAO c=new Common_DAO();
string recordName='test';
string searchType='test';
set<string> buttonsNameSet=new set<string> {'test'};
try{
Common_DAO.getTableComponentList(recordName);
} catch(DMLException e) {
system.assertEquals(e.getMessage(), e.getMessage());
}
Common_DAO.getTableSearchList(recordName,searchType);
Common_DAO.getTableActionList(buttonsNameSet);
}
You need to generate an exception from the try block to cover the lines of code in a catch block. One way to do that is to use System.runAs in your test class. The user that you specify in the runAs should not have any access to the Common_TableAction__c object. In this case, the SOQL will throw an exception and the code execution will go to catch block.
Thanks,
Ravi