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
BhaviBhavi 

TestMethod Problem

Hi there ,

 

    I used a following SOSL satement  to fill one of my project requirement

string searchText = ApexPages.currentPage().getParameters().get('sbstr');

if(searchText != '' && searchText!=null) { //Add the Wild Character

searchText = searchText + '*'; //Perform free text search on all the fields and capture

// records of suppliers

String searchquery='FIND \'' + searchText + '\' IN ALL FIELDS RETURNING Account('+FieldList+')';

List<List<SObject>> results = search.query(searchquery);

System.debug('hi'); integer j;

//Because SOSL return SObject List<List>, loop through, typecast and add the list to account list

for(j = 0;j<results.size();j++) {

AccountList.add((List<Account>)results[j]);

}

}

 

 

 

Its workin fine in controller class.

 

I am testing this case through method :

public static testmethod void testExportSuppliersConstructorForAllAccountIdWithSearchString() {

Account[] accountList = new Account[]{};

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

//Create a new instance of Account Object

Account account = new Account(Name = 'TestAccountName');

//add object in accountList

accountList.add(account);

}

//Insert the objects virtually

insert accountList;

//Set 'All' in the request parameter

ApexPages.currentPage().getParameters().put('Ids', 'All');

//Set the Search String Value in the request parameter

ApexPages.currentPage().getParameters().put('sbstr','TestAccountName' );

  //Start test from here

Test.startTest();

//Create a new instance of standard controller

ApexPages.StandardController sc = new ApexPages.standardController(new Account());

//Create an instance of the exportSupplier class

ExportSuppliers exportSuppliers = new ExportSuppliers(sc);

//Asserts for property sets in constructor of ExportSuppliers class

System.assert(exportSuppliers.getAccountList().size() >= 1);

  System.assert(exportSuppliers.getAccountList().get(0).size() >= 200);

//Stop test here

Test.stopTest();

}

 

but in this  testMethod my my second assert statement get failed because controller class returns 0(zero)

result.

I don't know why this is happening?

Can anybody please help me ?

Thanks in advance.

 

 

:Bhavi 

 

 

Message Edited by Ron Hess on 04-10-2009 08:55 AM
Best Answer chosen by Admin (Salesforce Developers) 
Ron HessRon Hess

testing search is tricky.  the records inserted are not instantly available in search,

since search terms are indexed by a seperate process in our database.

 

Here are the specific docs, and the trick to use ( taken from the docs) 

Adding SOSL Queries to Unit Tests

 

To ensure that test methods always behave in a predictable way, any Salesforce Object Search Language (SOSL) query that is added to an Apex test method returns an empty set of search results when the test method executes. If you do not want the query to return an empty list of results, you can use the Test.setFixedSearchResults system method to define a list of record IDs that are returned by the search. All SOSL queries that take place later in the test method return the list of record IDs that were specified by the Test.setFixedSearchResults method. Additionally, the test method can call Test.setFixedSearchResults multiple times to define different result sets for different SOSL queries. If you do not call the Test.setFixedSearchResults method in a test method, or if you call this method without specifying a list of record IDs, any SOSL queries that take place later in the test method return an empty list of results.

The list of record IDs specified by the Test.setFixedSearchResults method replaces the results that would normally be returned by the SOSL query if it were not subject to any WHERE or LIMIT clauses. If these clauses exist in the SOSL query, they are applied to the list of fixed search results. For example:

public class SoslFixedResultsTest1 {

public static testMethod void test
SoslFixedResults() {
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)];
}
}

Although the account record with an ID of 001x0000003G89h may not match the query string in the FIND clause ('test'), the record is passed into the RETURNING clause of the SOSL statement. If the record with ID 001x0000003G89h matches the WHERE clause filter, the record is returned. If it does not match the WHERE clause, no record is returned.