My Visualforce page is calling Controller methods using apex:actionFunction
and are not being executed while I am implementing the test class. Also in my test class I have test contact records which are not being fetched by the SOSL query from the Controller causing other methods to not executed.
Main controller(partial):
public Contact cont {get;set;}
public List<Contact> relatedConsList {get;set;}
//Constructor
public ParentController(ApexPages.StandardController stdController){
List<String> contactFields = new List<String>{'Name', 'Email'};
if (!Test.isRunningTest()) stdController.addFields(contactFields);
this.cont=(Contact)stdController.getRecord();
Contact currentCont = [Select Id, FirstName, LastName, Email FROM Contact WHERE Id =: cont.Id];
relatedConsList = ParentControllerHelper.searchRelatedContacts(currentCont.FirstName, currentCont.LastName, currentCont.Id, currentCont.Email);
}
public List<contactWrapper> processConts(){
if(contactList == null){
contactList = new List<contactWrapper>();
if(relatedConsList.size() > 0){
System.debug('Related List not Empty: ' +relatedConsList);
for(Contact c: relatedConsList){
contactList.add(new contactWrapper(c));
}
}
}
return contactList;
}
Helper class(partial):
Public static List<Contact> searchRelatedContacts(String firstName, String lastName, String contId, String email){
String searchTerm = '';
if(firstName != null){
if(lastName != null){
searchTerm += '('+firstName+' AND '+lastName+')' ;
} else {
searchTerm += firstName;
}
} else {
if(lastName != null) {
searchTerm += lastName;
}
}
List<Contact> relatedConsList = new List<Contact>();
relatedConsList = [FIND :searchTerm
IN ALL FIELDS
RETURNING Contact(
Name,Id,Email,Phone,MobilePhone
WHERE Email <> NULL AND Id <> :contId)][0];
System.debug('After query: ' +relatedConsList); //Getting as Empty in test execution all the time
return relatedConsList;
}
Test class:
@testSetup static void setup() {
List<Account> testAccts = new List<Account>();
for(Integer i=0;i<2;i++) {
testAccts.add(new Account(Name = 'TestAcct'+i));
}
insert testAccts;
List<Contact> testConts = new List<Contact>();
for (Integer i=0;i<10;i++){
testConts.add(new Contact(FirstName = 'Test', MiddleName= 'M'+i, LastName = 'lName', Email = '[email protected]'+i, AccountId=testAccts[0].Id));
}
testConts[4].AccountId = testAccts[1].Id; //Changing AccountId
testConts[5].AccountId = testAccts[1].Id;
testConts[6].AccountId = testAccts[1].Id;
testConts[7].AccountId = testAccts[1].Id;
insert testConts;
System.debug('Testsetup: ' +testConts[0].Id);
System.debug('Inserted Test Cons: ' +testConts);
}
@isTest
static void testMethodForCons(){
Contact con = [Select Id, FirstName, LastName, Email FROM Contact LIMIT 1];
List<Contact> testConsList = [SELECT Id, FirstName, LastName, Email
FROM Contact WHERE FirstName =: con.FirstName]; //I can query the test records.
Test.startTest();
PageReference pr = Page.myVFPage;
Test.setCurrentPageReference(pr);
ApexPages.currentPage().getParameters().put( 'Id', con.Id );
ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(con);
ParentController ctrl = new ParentController(sc);
Id [] fixedSearchResults= new Id[1];
fixedSearchResults[0] = con.Id;
Test.setFixedSearchResults(fixedSearchResults);
//The above statements doesn't help with the Controller's SOSL execution
ctrl.processConts(); //Not executing as SOSL not getting the test contacts list.
Test.stopTest();
}