+ Start a Discussion
justynjustyn 

Help - Testing of Apex Class

Hi

 

I've been able to tweek some code from a blog so that I have a fully functioning visualforce page search function in my  sandbox. I now want to deploy to the production environment, but it will not allow it as 'Average test coverage across all Apex Classes and Triggers is 70%, at least 75% test coverage is required.'

 

Not being a programmer (ie I can tweek but don't have a 'write it from scratch' background) this is now frustrating as the apex code and page work so nicely and will solve a major gripe in my organisation. 

 

Is anyone able to help? I have pasted the code below for ease. I am willing to learn, but recognise that there is a lot ot learn.

 

Justyn

 

 

public with sharing class ReferralSearchController {
 
  // the soql without the order and limit
  private String soql {get;set;}
  // the collection of contacts to display
  public List<Counselling_Practice__c> counselling_practices {get;set;}
 
  // the current sort direction. defaults to asc
  public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'asc'; } return sortDir;  }
    set;
  }
 
  // the current field to sort by. defaults to last name
  public String sortField {
    get  { if (sortField == null) {sortField = 'name'; } return sortField;  }
    set;
  }
 
  // format the soql for display on the visualforce page
  public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
    set;
  }
 
  // init the controller and display some sample data when the page loads
  public ReferralSearchController() {
    soql = 'select name, ID, member__c, Counselling_Service__c, Therapeutic_Approach__c, fee_amount__c, Referral_County__c, Practice_Area_Town__c, Practice_City_County__c, for_whom__c, gender__c, donations__c, negotiable__c, free_service__c from counselling_practice__c where name != null and currently_active__c = True';
    runQuery();
  }
 
  // toggles the sorting of query from asc<-->desc
  public void toggleSort() {
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query again
    runQuery();
  }
 
  // runs the actual query
  public void runQuery() {
 
    try {
      counselling_practices = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
    }
 
  }
 
  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
 
    String name = Apexpages.currentPage().getParameters().get('name');
    String ID = ApexPages.currentPage().getParameters().get('id');
    String member = Apexpages.currentPage().getParameters().get('member');
    String Counselling_Service = Apexpages.currentPage().getParameters().get('Counselling_Service');
    String Therapeutic_Approach = Apexpages.currentPage().getParameters().get('Therapeutic_Approach');
    String fee_amount = Apexpages.currentPage().getParameters().get('fee_amount');
    String Referral_County = Apexpages.currentPage().getParameters().get('Referral_County');
    String Practice_Area_Town = Apexpages.currentPage().getParameters().get('Practice_Area_Town');
    String Practice_City_County = Apexpages.currentPage().getParameters().get('Practice_City_County');
    String for_whom = Apexpages.currentPage().getParameters().get('for_whom');
    String gender = Apexpages.currentPage().getParameters().get('gender');
    String donations = Apexpages.currentPage().getParameters().get('donations');
    String negotiable = Apexpages.currentPage().getParameters().get('negotiable');
    String free_service = Apexpages.currentPage().getParameters().get('free_service');
 
    soql = 'select name, ID, member__c, Counselling_Service__c, Therapeutic_Approach__c, fee_amount__c, Referral_County__c, Practice_Area_Town__c, Practice_City_County__c, for_whom__c, gender__c, donations__c, negotiable__c, free_service__c from counselling_practice__c where name != null and currently_active__c = True';
    if (!name.equals(''))
      soql += ' and name LIKE \''+String.escapeSingleQuotes(name)+'%\'';
    if (!member.equals(''))
      soql += ' and member__c LIKE \''+String.escapeSingleQuotes(member)+'%\'';
     if (!Counselling_Service.equals(''))
      soql += ' and Counselling_Service__c includes (\''+Counselling_Service+'\')';
      if (!Therapeutic_Approach.equals(''))
      soql += ' and Therapeutic_Approach__c includes (\''+Therapeutic_Approach+'\')';
    if (!fee_amount.equals(''))
      soql += ' and fee_amount__c LIKE \''+String.escapeSingleQuotes(fee_amount)+'%\'';  
    if (!Referral_County.equals(''))
      soql += ' and Referral_County__c = \''+Referral_County+'\'';
    if (!Practice_Area_Town.equals(''))
      soql += ' and Practice_Area_Town__c LIKE \''+String.escapeSingleQuotes(Practice_Area_Town)+'%\'';
      if (!Practice_City_County.equals(''))
      soql += ' and Practice_City_County__c LIKE \''+String.escapeSingleQuotes(Practice_City_County)+'%\'';
    if (!for_whom.equals(''))
      soql += ' and for_whom__c includes (\''+for_whom+'\')';
    if (!gender.equals(''))
      soql += ' and gender__c LIKE \''+String.escapeSingleQuotes(gender)+'%\'';
      if (!donations.equals(''))
      soql += ' and donations__c LIKE \''+String.escapeSingleQuotes(donations)+'%\'';
    if (!negotiable.equals(''))
      soql += ' and negotiable__c LIKE \''+String.escapeSingleQuotes(negotiable)+'%\'';  
    if (!free_service.equals(''))
      soql += ' and free_service__c LIKE \''+String.escapeSingleQuotes(free_service)+'%\'';  
 
    // run the query again
    runQuery();
 
    return null;
  }
 
  // use apex describe to build the picklist values
  public List<String> Counselling_Service{
    get {
      if (Counselling_Service== null) {
 
        Counselling_Service = new List<String>();
        Schema.DescribeFieldResult field = counselling_practice__c.Counselling_Service__c.getDescribe();
 
        for (Schema.PicklistEntry f : field.getPicklistValues())
          Counselling_Service.add(f.getLabel());
 
      }
      return Counselling_Service;          
    }
    set;
  }
  
  public List<String> Therapeutic_Approach{
    get {
      if (Therapeutic_Approach== null) {
 
        Therapeutic_Approach = new List<String>();
        Schema.DescribeFieldResult field = counselling_practice__c.Therapeutic_Approach__c.getDescribe();
 
        for (Schema.PicklistEntry f : field.getPicklistValues())
          Therapeutic_Approach.add(f.getLabel());
 
      }
      return Therapeutic_Approach;          
    }
    set;
  }
  
  public List<String> Referral_County{
    get {
      if (Referral_County== null) {
 
        Referral_County = new List<String>();
        Schema.DescribeFieldResult field = counselling_practice__c.Referral_County__c.getDescribe();
 
        for (Schema.PicklistEntry f : field.getPicklistValues())
          Referral_County.add(f.getLabel());
 
      }
      return Referral_County;          
    }
    set;
  }
  public List<String> for_whom{
    get {
      if (for_whom== null) {
 
        for_whom = new List<String>();
        Schema.DescribeFieldResult field = counselling_practice__c.for_whom__c.getDescribe();
 
        for (Schema.PicklistEntry f : field.getPicklistValues())
          for_whom.add(f.getLabel());
 
      }
      return for_whom;          
    }
    set;
  }
 
}

 

jeremyyjeremyy

You have no test code, so your coverage is 0%, which when you attempt to deploy, brings your org's overall coverage below the minimum 75% level.

 

See Testing Apex

justynjustyn

Hi

 

Thanks for the feedback. I thought that might be the case. Am going to have to get a lot of coffee and read the apex test info big time. One quick question is if the testing code goes into my existing apex class or as a separate class?

 

Justyn

jeremyyjeremyy

I generally put test code in a separate class. If you mark the test class with the @isTest annotation, it doesn't count against code size limits.