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
Tristan PTristan P 

Writing a test class for CustomSearchController

Hi all,

I'm attempting to deploy the CustomSearchController class as written in the official developer documentation here:

https://developer.salesforce.com/docs/atlas.en-us.communities_dev.meta/communities_dev/communities_dev_example_search.htm
 
public class CustomSearchController {
	@AuraEnabled
	public static List<String> searchForIds(String searchText) {
		List<List<SObject>> results = [FIND :searchText IN ALL FIELDS  RETURNING Account(Id), Campaign(Id), 		Contact(Id), Lead(Id)];
		List<String> ids = new List<String>();
		for (List<SObject> sobjs : results) {
			for (SObject sobj : sobjs) {
				ids.add(sobj.Id);
			}
		}
		 return ids;
	}
}

However I can't see any instruction for writing the test class required. Can anyone please help with this?

Thanks,

Tristan
Best Answer chosen by Tristan P
Sharat C 2Sharat C 2
Hi Tristan,

Use the below to get the coverage.
 
@istest
public class CustomSearchController_Test {
	
    @testSetup static void setup() {
        // Create common test accounts
        List<Account> testAccts = new List<Account>();
        for(Integer i=0;i<2;i++) {
            testAccts.add(new Account(Name = 'Test '+i));
        }
        insert testAccts;
		
        Campaign ca = new campaign();
        ca.Name = 'Test';
        ca.IsActive = true;
        insert ca;
        
        Lead ll = new Lead();
        ll.LastName = 'Test';
        ll.Company = 'Deloitte';
        insert ll;
        
        Contact con = new Contact();
        con.LastName = 'Test';
        Insert Con;        	        
    }
    
    @isTest static void testMethod1() {
               
        Id [] fixedSearchResults= new Id[4];
        fixedSearchResults[0] = [Select Id,Name from Contact Limit 1].Id;
        fixedSearchResults[1] = [Select Id,Name from Account Limit 1].Id;
        fixedSearchResults[2] = [Select Id,Name from Lead Limit 1].Id;
        fixedSearchResults[3] = [Select Id,Name from Campaign Limit 1].Id;
        Test.setFixedSearchResults(fixedSearchResults);
                
        List<String> lla = CustomSearchController.searchForIds('Test');
        
        system.assertEquals(false, lla.isEmpty());
    }
}

Thanks
Sharat​​​​​​​

All Answers

Sharat C 2Sharat C 2
Hi Tristan,

Use the below to get the coverage.
 
@istest
public class CustomSearchController_Test {
	
    @testSetup static void setup() {
        // Create common test accounts
        List<Account> testAccts = new List<Account>();
        for(Integer i=0;i<2;i++) {
            testAccts.add(new Account(Name = 'Test '+i));
        }
        insert testAccts;
		
        Campaign ca = new campaign();
        ca.Name = 'Test';
        ca.IsActive = true;
        insert ca;
        
        Lead ll = new Lead();
        ll.LastName = 'Test';
        ll.Company = 'Deloitte';
        insert ll;
        
        Contact con = new Contact();
        con.LastName = 'Test';
        Insert Con;        	        
    }
    
    @isTest static void testMethod1() {
               
        Id [] fixedSearchResults= new Id[4];
        fixedSearchResults[0] = [Select Id,Name from Contact Limit 1].Id;
        fixedSearchResults[1] = [Select Id,Name from Account Limit 1].Id;
        fixedSearchResults[2] = [Select Id,Name from Lead Limit 1].Id;
        fixedSearchResults[3] = [Select Id,Name from Campaign Limit 1].Id;
        Test.setFixedSearchResults(fixedSearchResults);
                
        List<String> lla = CustomSearchController.searchForIds('Test');
        
        system.assertEquals(false, lla.isEmpty());
    }
}

Thanks
Sharat​​​​​​​
This was selected as the best answer
Tristan PTristan P
Thanks Sharat! This has done the job.