+ Start a Discussion
clstanton007clstanton007 

Test Class for a Standard Controller Extension

Hello,

 

I have written a few test classes for triggers and such, but am having difficulty with my test class for controller extension.  This is how the app works:  There is a VF page with 4 picklists, at least one is required.  Then user presses the Search button, and the extension performs the search (using the values of the picklists in the SOQL). 

 

Here's the code for the class:

 

 

public with sharing class BuyerSupplier {
         
    private ApexPages.StandardController controller {get; set;}
    public List<Buyer__c> searchResults {get;set;}
    public string searchText {get;set;}
  
    public String site {get; set;}
    public String region {get;set;}
    public String division {get;set;}
    public String category {get;set;}
    //public String hiddenSite {get; set;}
    
    public Boolean needsAnd;
 
    // standard controller
    public BuyerSupplier(ApexPages.StandardController controller) {  }
 
    // fired when the search button is clicked
    public PageReference search() {
        
        needsAnd = false;
        
        site = ApexPages.currentPage().getParameters().get('hiddenSite');
        region = ApexPages.currentPage().getParameters().get('hiddenRegion');
        division = ApexPages.currentPage().getParameters().get('hiddenDivision');
        category = ApexPages.currentPage().getParameters().get('hiddenCategory');
        
        //FOR SOME REASON, VALUES OF PICKLISTS ARE COMING OVER AS "__".  IF THAT HAPPENS, REMOVE THEM.
        if(site == '__') site = '';
        if(region == '__') region = '';
        if(division == '__') division = '';
        if(category == '__') category = '';

        
        //FIRST CHECK TO SEE IF AT LEAST ONE FIELD IS COMPLETED
        if((site == '' || site == '__') && (region == '' || region == '__') && (division == '' || division == '__') && (category == '' || category == '')) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please make a selection for at least ONE of the fields below.'));
            return null;
        }        
        
        String qry = 'select Buyer__c, Buyer_Email__c, Category__c, Division__c, Region__c, Supplier__c, Site__c, Supplier_URL__c from Buyer__c where ';
        
        if(site != '') {
            if (needsAnd == true) {
                qry = qry + 'and Site__c = \'' + site + '\' ' ;
            }
            
            else {
                qry = qry + 'Site__c = \'' + site + '\' ' ;
                needsAnd = true;
            }
        }
    
        if(region != '') {
            if (needsAnd == true) {
                qry = qry + 'and Region__c = \'' + region + '\' ' ;
            }
            
            else {
                qry = qry + 'Region__c = \'' + region + '\' ' ;
                needsAnd = true;
            }
        }
        
        if(division != '') {
            if (needsAnd == true) {
                qry = qry + 'and Division__c = \'' + division + '\' ';
            }
            
            else {
                qry = qry + 'Division__c = \'' + division + '\' ' ;
                needsAnd = true;
            }
        }
        
        if(category != '') {
            if (needsAnd == true) {
                qry = qry + 'and Category__c = \'' + category + '\' ' ;
            }
            
            else {
                qry = qry + 'Category__c = \'' + category + '\' ' ;
                needsAnd = true;
            }
        }
    
        qry = qry + ' order by Site__c, Region__c, Division__c, Category__c Limit 1000';
        
        searchResults = Database.query(qry);
        return null;
  }
}

 

 

Then here is the code for the Test Class so far.  I get the following error:  Error: Compile Error: Invalid type: BuyerSupplierExt at line 71 column 42.

 

@isTest
private class BuyerSupplier_Test {

        private Buyer__c buyer;  
        
        static testmethod void testSearchBuyers(){
        
            // Create dummy data for test purposes.
            Buyer__c b = new Buyer__c(
                Region__c = 'Americas',
                Site__c = 'Rochester',
                Division__c = 'Vision Care',
                Category__c = 'Advertising',
                Buyer__c = 'Test Buyer',
                Supplier__c = 'Test Supplier');
            
            System.debug('******************Inserting the test buyer record...');
            insert b;
            
            ApexPages.StandardSetController controller = new ApexPages.StandardSetController(b); 
            BuyerSupplierExt bsExt = new BuyerSupplierExt(controller);
    
            //Call controller.search() and assert the list contains the expected records.
            List<Buyer__c> results = bsExt.search();
            //List<Buyer__c> results = [select Region__c, Site__c, Division__c, Category__c, Buyer__c from Buyer__c where Site__c = 'Rochester' and Division__c = 'Vision Care' and Category__c = 'Advertising'];
            
            system.assertEquals(results.size(),1);
    
        }           
}

 

Any help would be appreciated!!

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
gv007gv007

BuyerSupplierExt bsExt = new BuyerSupplierExt(controller);

 

I think there is no class called BuyerSupplierExt in yours application

 

use BuyerSupplier instead of BuyerSupplierExt.

 

All Answers

gv007gv007

BuyerSupplierExt bsExt = new BuyerSupplierExt(controller);

 

I think there is no class called BuyerSupplierExt in yours application

 

use BuyerSupplier instead of BuyerSupplierExt.

 

This was selected as the best answer
clstanton007clstanton007

That did it!  Thanks for your reply - really appreciate it!!

 

C