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
VRKVRK 

unable to write test class for SOSL searchText

Hi i am unable to write test classs for SOSL searchText ...below is my apex class
public class case_exn {
   public Case id{get;set;}
   public String searchText{get;set;}
   List<Case> results = new List<Case>();
   public Boolean IsOwnerSpecific {get;set;}  
   private ApexPages.StandardController ctrl
 public case_exn(ApexPages.StandardController controller)
  {
  
     id = (Case)controller.getRecord();
   
  }
    public List<SelectOption> getItems() {

        List<SelectOption> options = new List<SelectOption>();
       
        options.add(new SelectOption('Admin mailbox','Admin Mailbox'));
        options.add(new SelectOption('Support Mailbox','Support  Mailbox'));
}
public String getSearchText() 
    {
      return searchText;
   }
 
   public void setSearchText(String s) {
      searchText = s;
   }
 
   public List<Case> getResults() 
   {
      return results;
   }
public pageReference search() {
results = (List<Case>)[FIND :searchText RETURNING Case(CaseNumber,Type, Subject, Priority,Status,CreatedDate,SLA_Flag__c,Owner.Name ];
  }

my Test clas:
@isTest
public class TestsearchCaseVFPage {
 private static testMethod void testSoslFixedResults()
 {
  Case obj=new Case(Type='test',Subject='selfService',Priority='medium',Status='Inprogress');
  insert obj;  
  ApexPages.StandardController sc = new ApexPages.StandardController(obj);
  case_exn testAccPlan = new case_exn(sc); 
Id [] fixedSearchResults= new Id[testAccPlan.Id];
      Test.setFixedSearchResults(fixedSearchResults);
       
      //test for searching account in list
        Test.startTest();
        results = SoslService.search(obj.searchText);
        Test.stopTest();
//assertion
  System.assertEquals(testAccPlan.Id, results[0][0].Id, 'Account Ids should match');
}

But test class Code not working  and coverage also very low....can u pls check and help on this

REgard
sekhar
Best Answer chosen by VRK
Raj VakatiRaj Vakati
Use this code . Replace YOUR_SEARCH_PAGE with your visualforce page
 
@isTest
public class TestsearchCaseVFPage {
    private static testMethod void testSoslFixedResults()
    {
        Case obj=new Case(Type='test',Subject='selfService',Priority='medium',Status='Inprogress');
        insert obj;  
        
        ApexPages.StandardController sc = new ApexPages.StandardController(obj);
        case_exn caseextCon = new case_exn(sc); 
        
        PageReference pageRef = Page.YOUR_SEARCH_PAGE;
        pageRef.getParameters().put('id', String.valueOf(obj.Id));
        Test.setCurrentPage(pageRef);
        
        
        Test.startTest();
        caseextCon.getItems();
        caseextCon.setSearchText('Salesforce');
        caseextCon.getSearchText();
        caseextCon.search();
        caseextCon.getResults();
        
        Test.stopTest();
    }
}

 

All Answers

Raj VakatiRaj Vakati
Use this code . Replace YOUR_SEARCH_PAGE with your visualforce page
 
@isTest
public class TestsearchCaseVFPage {
    private static testMethod void testSoslFixedResults()
    {
        Case obj=new Case(Type='test',Subject='selfService',Priority='medium',Status='Inprogress');
        insert obj;  
        
        ApexPages.StandardController sc = new ApexPages.StandardController(obj);
        case_exn caseextCon = new case_exn(sc); 
        
        PageReference pageRef = Page.YOUR_SEARCH_PAGE;
        pageRef.getParameters().put('id', String.valueOf(obj.Id));
        Test.setCurrentPage(pageRef);
        
        
        Test.startTest();
        caseextCon.getItems();
        caseextCon.setSearchText('Salesforce');
        caseextCon.getSearchText();
        caseextCon.search();
        caseextCon.getResults();
        
        Test.stopTest();
    }
}

 
This was selected as the best answer
VRKVRK
Thanks Raj for your quick help..its working and code coverage 80%  Thanks