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
kcharubinkcharubin 

Testing Controller Extension for Visualforce Page

Hello

I've recently been writting a few basic controllers for some visualforce pages, and of course now  need to test them. Problem is I don't really know how. Normally you know you'd instantiate the class, pass data to the methods, and assert against the results right?  With this controller I dont know what to test against.

 

It would be great if someone could help me write a test calss for this

 

public class MyLeadsController {

    public List<Consumer_Leads__c> unassignedLeads {get; set;}
    public List<Consumer_Leads__c> allLeads {get; set;}
    public String leadCenter {get; set;}
    
    public MyLeadsController(ApexPages.StandardController controller) {
    
        leadCenter = ApexPages.currentPage().getParameters().get('lc');
        
        this.allLeads = [select id, First_Name__c, Last_Name__c, State__c, Lead_Request__c, IFP_Producer_Receiving_the_Lead__c, Lead_Center__c
        from Consumer_Leads__c 
        where Lead_Center__c = :leadCenter AND IFP_Producer_Receiving_the_Lead__c != ''
        order by id]; 
        
        this.unassignedLeads = [select id, First_Name__c, Last_Name__c, State__c, Lead_Request__c, IFP_Producer_Receiving_the_Lead__c, Lead_Center__c 
        from Consumer_Leads__c
        where Lead_Center__c = :leadCenter AND IFP_Producer_Receiving_the_Lead__c = ''
        order by id];
    }
    


    public PageReference Save(){
        update allLeads;
        update unassignedLeads;
        return null;
    }   
    
    public PageReference Back(){
        return new PageReference('/'+leadCenter);
    } 
   
}

 

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi,

 

Below is sample code for your Test class.

 

Test Class:

 

@istest
public class TestMyLeadsController{
    Private Static testmethod void TestMyLeadsController(){    
		Lead_Center__c objLeadCenter = new Lead_Center__c();
		objLeadCenter.Name = 'Test';
		insert objLeadCenter;
		
		Consumer_Leads__c objConsumerLeads = new Consumer_Leads__c();
		objConsumerLeads.Name = 'test';
		objConsumerLeads.First_Name__c = 'Test Fname';
		objConsumerLeads.Last_Name__c = 'Test Lname';
		objConsumerLeads.IFP_Producer_Receiving_the_Lead__c = 'test';
		objConsumerLeads.Lead_Center__c = objLeadCenter.id;
		insert objConsumerLeads;
		
		
		ApexPages.currentPage().getParameters().put('lc',objLeadCenter.id);
		ApexPages.StandardController stdLead = new ApexPages.StandardController(objLeadCenter);
	    MyLeadsController objMyLeadsController  = new MyLeadsController(stdLead);
		
		objMyLeadsController.Save();
		objMyLeadsController.Back();
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

 

All Answers

hitesh90hitesh90

Hi,

 

Below is sample code for your Test class.

 

Test Class:

 

@istest
public class TestMyLeadsController{
    Private Static testmethod void TestMyLeadsController(){    
		Lead_Center__c objLeadCenter = new Lead_Center__c();
		objLeadCenter.Name = 'Test';
		insert objLeadCenter;
		
		Consumer_Leads__c objConsumerLeads = new Consumer_Leads__c();
		objConsumerLeads.Name = 'test';
		objConsumerLeads.First_Name__c = 'Test Fname';
		objConsumerLeads.Last_Name__c = 'Test Lname';
		objConsumerLeads.IFP_Producer_Receiving_the_Lead__c = 'test';
		objConsumerLeads.Lead_Center__c = objLeadCenter.id;
		insert objConsumerLeads;
		
		
		ApexPages.currentPage().getParameters().put('lc',objLeadCenter.id);
		ApexPages.StandardController stdLead = new ApexPages.StandardController(objLeadCenter);
	    MyLeadsController objMyLeadsController  = new MyLeadsController(stdLead);
		
		objMyLeadsController.Save();
		objMyLeadsController.Back();
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

 

This was selected as the best answer
kcharubinkcharubin

Thanks for the quick help. I modified it a little since I had some validation rules in place on Consumer_Leads__c.

I thought that in testMethod there needs to be System.assertEquals() and such.