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
JosephDJosephD 

Help with testing APEX search code

Hi.  I am brand new to APEX, and well, programming in general.  I create a VF page to search my custom tabe, Facilities__c.    I am trying to create test code for the APEX class but am not making any progress.


Here is a start at my test code which is incomplete and probably incorrect:

***********************************

@isTest
private class TestFacilitySearch {

static testMethod void testFacilitySearchController() {
   Facilities__c dcname;
   ApexPages.StandardController sc;
   FacilitySearchController fsc;
   
   Facilities__c facname = new Facilities__c(name= 'DCTest', Agency_Name__c = 'Joe 1');
   insert facname;
   

   fsc.searchText = '';
   fsc.search();
   }

}

***********************************

 

Here is my APEX class.  The interface works fine on the VF page.

***********************************

public class FacilitySearchController {
 
    // the results from the search. do not init the results or a blank rows show up initially on page load
    public List<FacilityWrapper> searchResults {get;set;}
    // the categories that were checked/selected.
    public List<FacilityWrapper> selectedCategories {
        get {
            if (selectedCategories == null) selectedCategories = new List<FacilityWrapper>();
            return selectedCategories;
        }
        set;
    }      
 
    // the text in the search box
    public string searchText {
        get {
            if (searchText == null) searchText = ''; // prefill the serach box for ease of use
            return searchText;
        }
        set;
    }
 
    // constructor
    public FacilitySearchController() {}
 
    // fired when the search button is clicked
    public PageReference search() {
 
        if (searchResults == null) {
            searchResults = new List<FacilityWrapper>(); // init the list if it is null
        } else {
            searchResults.clear(); // clear out the current results if they exist
        }
        // Note: you could have achieved the same results as above by just using:
        // searchResults = new List<FacilityWrapper>();
 
        // dynamic soql for fun
        String qry = 'Select c.Name, c.Agency_Name__c, c.Component_Name__c From Facilities__c c Where c.Name LIKE \'%'+searchText+'%\' Order By c.Name';
        // may need to modify for governor limits??
        for(Facilities__c c : Database.query(qry)) {
            // create a new wrapper by passing it the category in the constructor
            FacilityWrapper cw = new FacilityWrapper(c);
            // add the wrapper to the results
            searchResults.add(cw);
        }
        return null;
    }   
 
    public PageReference next() {
 
        // clear out the currently selected categories
        selectedCategories.clear();
 
        // add the selected categories to a new List
        for (FacilityWrapper cw : searchResults) {
            if (cw.checked)
                selectedCategories.add(new FacilityWrapper(cw.cat));
        }
 
        // ensure they selected at least one category or show an error message.
        if (selectedCategories.size() > 0) {
            return Page.FacilityResults;
        } else {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select at least one Facility.'));
            return null;
        }       
 
    }       
 
    // fired when the back button is clicked
    public PageReference back() {
        return Page.FacilitySearch;
    }       
 
}

***********************************

 

 

 

Thanks for any help!  I greatly appreciate it.

dmchengdmcheng

This is a good place to start:

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

The Visualforce Controllers section will show you how to instantiate the controller correctly in your test code, and also how to instantiate your VF page in the test.