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
richardvrichardv 

Testing exception handling for SOSL searches

 

I have the following code -

 

====================================================

1        try{

2         List<List<SObject>> searchList = [

3         FIND :searchString IN ALL FIELDS RETURNING 

4         Account (id,IsPartner,name WHERE IsPartner = true ORDER BY name)

5 ];

6 searchResults = ((List<Account>)searchList[0]);

7        }catch(QueryException e){

8         errorTooManySearchResults = true;

9         return null;

10       }catch(Exception e1){

11 ApexPages.addMessages(e1);

12         return null;

13       }        

====================================================

 

-and I'm trying to get 100% test coverage.  Testing lines 1-6 are easy of course; however, I've been unable to figure out a way to test lines 7-12.  A generic Exception should be thrown if :searchString is less that two characters.  I try setting that value to less than two characters but the testing environment seems to ignore that.  Any ideas on forcing generic Exception to be thrown?

 

As far as testing QueryException, the reason I'm catching QueryException is it will be thrown if too many results are returned; and this works great through the UI.  In order to test that, I need to be able to create whatever number of accounts is needed to go over the governor limit.  With the below test, I create 250; however, the SOSL query above only returns 200.  Any ideas forcing the query to return too many rows?

 

 

====================================================

    private static testmethod void testTooManyResultsError() {

List<Account> accounts = new List<Account>();

for(Integer i = 0; i < 250; i++){

accounts.add(new Account(name='Test' +i));

}

insert accounts;

Id[] ids = new Id[accounts.size()];    

for(Integer i = 0; i < accounts.size(); i++){

accounts[i].IsPartner = true;

ids[i] = accounts[i].id;

}

update accounts;

        Test.setFixedSearchResults(ids);

 

        TheController controller = new TheController();

        System.assertEquals(false, controller.errorTooManySearchResults); 

        System.assertEquals(controller.doPageLoad(),null); 

        System.assertEquals(false, controller.errorTooManySearchResults); 

        

        controller.searchString = 'test';

 

        System.assertEquals(controller.doSearch(),null); 

        System.assertEquals(true, controller.errorTooManySearchResults); 

    }

====================================================