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
Jonathan Wolff 7Jonathan Wolff 7 

test Class for Search Component Apex Class

Hello,
I need a test class for a component I made. Could you help me with it?


APEX CLASS

public class MediathekSearchController {

 
    @AuraEnabled(cacheable=true)
    public static List<List<sObject>> getSearchResult(String searchKey){
     
        List<List<sObject>> searchResult = [FIND :searchKey
                                            IN ALL FIELDS RETURNING
                                            Mediathek__c (Id, Name)
                                            ];
        return searchResult;
     
    }
}
Best Answer chosen by Jonathan Wolff 7
AbhinavAbhinav (Salesforce Developers) 
Hi Jonathan,

You can try below sample code
 
@isTest
public class Test_MediathekSearchController {
    
    @isTest static void getSearchResultTest() {
        
        String searchKey ='test';
        MediathekSearchController.getSearchResult(searchKey);
    }

}

If it helps mark it as best answer.

Thanks!​​​​​​​

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Jonathan,

You can try below sample code
 
@isTest
public class Test_MediathekSearchController {
    
    @isTest static void getSearchResultTest() {
        
        String searchKey ='test';
        MediathekSearchController.getSearchResult(searchKey);
    }

}

If it helps mark it as best answer.

Thanks!​​​​​​​
This was selected as the best answer
CharuDuttCharuDutt
Hii Jhon
Try Below Code
@isTest
public class MediathekSearchControllerTest { 
    @isTest static void UnitTest() {

        MediathekSearchController.getSearchResult('FieldName');
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 
Suraj Tripathi 47Suraj Tripathi 47

Hi,

You can take references from the below code.


List<Contact> retContacts = [FIND :txtName IN ALL FIELDS RETURNING Contact(Id, Name,Phone,Email)][0];
public class SoslFixedResultsTest1 {

    public static testMethod void testSoslFixedResults() {
       Id [] fixedSearchResults= new Id[1];
       fixedSearchResults[0] = '001x0000003G89h';
       Test.setFixedSearchResults(fixedSearchResults);
       List<List<SObject>> searchList = [FIND 'test' 
                                         IN ALL FIELDS RETURNING 
                                            Account(id, name WHERE name = 'test' LIMIT 1)];
    }
}

https://www.infallibletechie.com/2019/06/adding-sosl-queries-to-unit-tests.html

Please mark it as the Best Answer so that other people would take references from it.

Thank You