0
@isTest private class cSearchTest{
@isTest static void testbuildQuery(){
    Profile p = [select id from profile where name='Standard User'];

     User u = new User(alias = 'standt', email='standarduser@testorg.com',
     emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
     localesidkey='en_US', profileid = p.Id,
     timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');

     System.runAs(u){
         //system.debug(conSearch.buildQuery());
         system.debug('Tree Types are: ' + conSearch.getTreeType());
     }

}

}

The above code causes the error:

Method does not exist or incorrect signature: conSearch.getTreeType()

The code from the conSearch controller

public class conSearch {

public PageReference doFilter() {
    return null;
}

public List<Tree2__c> sTree {get;set;}
public String searchString {get;set;}

public Tree2__c tSurgeon {get;set;}

public String searchType {get;set;}
public String searchDisease {get;set;}
public String searchSurgeon {get;set;}
public String searchStatus {get;set;}
public boolean searchBreed {get;set;}
public Date searchDate {get;set;}

String startQuery = 'SELECT TreeID__c, Tree2__c.Name, Status__c FROM Tree2__c WHERE Native_Breed__c = :searchBreed';
List<String> query = new List<String>();
public String testQuery {get;set;}

public void buildQuery(){
    Date phDate;
    searchSurgeon = tSurgeon.Surgeon__c;

    String tyQuery = 'Type_of_Tree__c LIKE :searchType';
    String diQuery = 'Disease__c LIKE :searchDisease';
    String suQuery = 'Surgeon__c = :searchSurgeon';
    String stQuery = 'Status__c LIKE :searchStatus';
    String daQuery = 'Date_of_planting__c = :searchDate';
    String brQuery = 'Native_Breed__c = :searchBreed';

    query.add(startQuery);

    if(searchDisease != null){
        query.add(diQuery);
    }
    if(searchSurgeon != null){
        query.add(suQuery);
    }
    if(searchStatus != null){
        query.add(stQuery);
    }
    if(searchDate != phDate){
        query.add(daQuery);
    }
    if(searchType != null){
        query.add(tyQuery);
    }

    String result = String.join( query, ' AND ' );
    sTree = Database.query(result);
    testQuery = result;
    query.clear();
}

public void clear(){
    sTree.clear();
}

public conSearch(ApexPages.StandardController controller){
    rTreeType = (Tree2__c) controller.getRecord();
    rDisease = (Tree2__c) controller.getRecord();
    rStatus = (Tree2__c) controller.getRecord();

    tSurgeon = new Tree2__c();
}

//**-------------------------------------------populate picklist values-------------------------------------------**//
Tree2__c rTreeType;
Tree2__c rDisease;
Tree2__c rStatus;

public List<SelectOption> getTreeType(){
    List<SelectOption> treeType = new List<SelectOption>();

    Schema.DescribeFieldResult dfr = Tree2__c.Type_of_Tree__c.getDescribe();
    for(Schema.PicklistEntry ple : dfr.getPicklistValues()){
        treeType.add(new SelectOption(ple.getValue(), ple.getLabel(), !ple.isActive()));
        if(ple.isDefaultValue()){
            rTreeType.Type_of_Tree__c = ple.getValue();
        }
    }
    return treeType;
}

public List<SelectOption> getDisease(){
    List<SelectOption> disease = new List<SelectOption>();

    Schema.DescribeFieldResult dfr = Tree2__c.Disease__c.getDescribe();
    for(Schema.PicklistEntry ple : dfr.getPicklistValues()){
        disease.add(new SelectOption(ple.getValue(), ple.getLabel(), !ple.isActive()));
        if(ple.isDefaultValue()){
            rDisease.Disease__c = ple.getValue();
        }
    }
    return disease;
}

public List<SelectOption> getStatus(){
    List<SelectOption> status = new List<SelectOption>();

    Schema.DescribeFieldResult dfr = Tree2__c.Status__c.getDescribe();
    for(Schema.PicklistEntry ple : dfr.getPicklistValues()){
        status.add(new SelectOption(ple.getValue(), ple.getLabel(), !ple.isActive()));
        if(ple.isDefaultValue()){
            rStatus.Status__c = ple.getValue();
        }
    }
    return status;
}  

}

Am i incorrectly calling the class? As far as I can this is the correct way to call the method in a unit test.

  • can u share ur complete code including the actual class you are trying to test.. where is this object conSearch being created ? – Vamsi Krishna Jun 25 '15 at 14:22
  • 2
    If getTreeType is not a static method you need to instantiate the class first before you can call the method. Please post all relevant code. – Eric Jun 25 '15 at 14:26
2

In your test you will need to instantiate the class. This will require a Standard Controller as well. So to get started your test will look like this:

@isTest private class cSearchTest{
@isTest static void testbuildQuery(){
    Profile p = [select id from profile where name='Standard User'];

     User u = new User(alias = 'standt', email='standarduser@testorg.com',
     emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
     localesidkey='en_US', profileid = p.Id,
     timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
     //Instantiate class here with default Tree2__c object
     conSearch con = New conSearch(New ApexPages.StandardController(New tree2__c()));

     System.runAs(u){
         //system.debug(conSearch.buildQuery());
         system.debug('Tree Types are: ' + con.getTreeType());
     }

}

}

You will need to ensure the New Tree2__c() part is instantiated with the values you require. This does not address any other potential issue so if this answer solves the stated problem mark it as answered and create a new question for any other issues that arise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.