+ Start a Discussion
Patrick ConnerPatrick Conner 

Custom Search Test Class

I'm trying to create a custom search page. The page and controller work well, but I would appreciate any help with the test class (currently only have 52% coverage). 


CONTROLLER:

public with sharing class WildcardSearchController{
  
// the soql without the order and limit
  private String soql_C {get;set;}  
// the collection of contributions to display
  public List<contact> contributions {get;set;} 
// This is the base soql for building the query
  public String soqlBase_C {
    get{ return soqlBase_C ; }
    set;
   } 
 // Create a query variable for debugging
  public String theQuery{
   get {return theQuery; }
   set;
   }
// Create a variable for record returned count
  Public Integer numberContributions{
   get ;
   set;
   }
// Create a STRING variable for record returned count
  Public string numberContributionsStr{
    get{ return numberContributionsStr; }
   set;
   }
// a set of variables to persist the search parameters   
   Public string Name{ 
       get {return Name; }
       set ;
       }
   Public string Email{ 
       get {return Email; }
       set; 
       }
   Public string Phone{ 
       get {return Phone; }
       set; 
       }
   Public string soqlLimitPage{ 
       get {return soqlLimitPage; }
       set; 
       }
                                                     // END OF VARIABLE Declarations
   
  // init the controller and display some sample data when the page loads CONTRUCTOR
  public WildcardSearchController() {
    soqlBase_C = 'Select Name, Email, Phone from Contact where Phone !=null ';
    system.debug(soqlBase_C);
    soql_C = soqlBase_C;
    runQuery();
  }                                                  // end CONTRUCTOR
  // runs the actual query
  public void runQuery() {     
    try {                                            // Get Contribution Records
      if(soqlLimitPage == null) {
        soqlLimitPage = 'limit 30';
        system.debug('TRY set null soqlLimitPage = ' + soqlLimitPage );
        }
      theQuery = soql_C + ' ' + soqlLimitPage;
      system.debug(' theQuery = ' + theQuery);  
      contributions = Database.query(theQuery);
      system.debug('Contributions list returned = ' + contributions);  
      numberContributions = contributions.size();
      numberContributionsStr = String.valueOf(numberContributions );  
    }               // end try
    catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, string.valueof(e))); 
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, soqlLimitPage));
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, theQuery));
    }              // end catch
  }                // end runQuery
 
  // runs the search with parameters passed via variables
  public PageReference runSearch() {
                                         // Display search variables
    system.debug('Name = ' + Name );
    system.debug('Email = ' + Email );
    system.debug('Phone = ' + Phone);
    system.debug('soqlLimitPage= ' + soqlLimitPage);
                                        // Build the rest of the soql_ by tacking on and operator parameter  
    soql_C = soqlBase_C;                                          
    if (!Name.equals(''))
      soql_C += ' and Name  LIKE \'%'+String.escapeSingleQuotes(Name )+'%\'';
    if (!Email.equals(''))
      soql_C += ' and Email LIKE \'%'+String.escapeSingleQuotes(Email )+'%\'';
    if (!Phone.equals(''))
      soql_C += ' and Unformatted__c LIKE \'%'+String.escapeSingleQuotes(Phone )+'%\'';
                                         // now, go run the query again and exit
    runQuery();
    return null;
  }                // end runSearch
  
  
  // tests the variables, only function is testing
  Public void runPicklists() {
      List<String> junkList;
      String junkString;      
      junkString = soqlLimitPage;
  }              // end runPicklists   
}                // end class


TEST CLASS

@isTest
private class Test_WildcardSearchController
{

    static testMethod void testController()
    {
        WildcardSearchController CSBC=new WildcardSearchController();
        csbc.Name='bob';
        csbc.Email='gmail';
        csbc.Phone='0299';
        csbc.runQuery();
        
       
       csbc.soqlBase_C= 'select Name, Email, Phone FROM Contact Where Name !=null';
        System.assertEquals(csbc.soqlBase_C, 'select Name, Email, Phone FROM Contact Where Name !=null');
        
        csbc.runQuery();
        

    }
    
    //instantiate the page
    static testMethod void WildcardSearch(){
        PageReference pg = Page.WildcardSearch;
        
        Test.setCurrentPage(pg);
        pg.getParameters().put('Contact.Name', 'bob');
        pg.getParameters().put('Email', 'gmail');
        pg.getParameters().put('Phone', '0299');

        
        //instantiate the controller
        WildcardSearchController controller=new WildcardSearchController();
     
        
        controller.runQuery();

    }
    
}

Thanks!!
Patrick ConnerPatrick Conner
Looking at it posted here, I realize how redundant and sometimes extraneous parts of the test class are. Ignoring that, I'm still unsure how to design a test for all variables in my controller. Sorry for the sloppiness in the test class, but thanks again for any help.
Anoop yadavAnoop yadav
Hi,

First, you should insert Contact record in test class.

Check the below link for more information.
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods