On doing a sosl search with just one character search parameter, I am getting an exception. To overcome this i have used custom error message in the catch block.However I am unable to cover the catch block in the test class. The functionality of the code is working fine.
CONTROLLER METHOD:
public void Query(){
try{
iserrorfound = false;
if (sSearchWord != null && sSearchWord.trim() != '')
{
List < List < sObject >> SearchResult = search.query('FIND \'' + string.escapeSingleQuotes(sSearchWord) + '\' IN ALL FIELDS RETURNING Object__c (ID,name,field1__c,field2__c '+ addlFunc() +' )');
belts = SearchResult[0];
SetController= new ApexPages.StandardSetController(SearchResult[0]);
}
else
{
//Logic if search parameter is null/empty
}
SetController.setPageSize(10);
}
catch(Exception e)
{
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Please enter at least two characters');
ApexPages.addMessage(myMsg);
iserrorfound = true;
}
}
TEST CLASS:
test.starttest()
//
Inst.sSearchWord = 'p';
Inst.Query();
test.stoptest()