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
GstartGstart 

Help in writing test class for Custom Controller

I have shared my custom controller class and I need some help on writing test class for the same. Kindly let me know how to proceed
public class UserAccountsExtension {        
    //Variable Declaration
    boolean showReg=true;
    public UserAccount__c uAccount{get;set;} 
    public Employee_Detail__c addEmployee{get;set;}  
    public List<Employee_Detail__c> uEmployeeDetail{get;set;}      
    
    public UserAccountsExtension(){                
        
        Id id = ApexPages.currentPage().getParameters().get('id'); 
        String pgName=ApexPages.currentPage().getUrl().substringAfter('/apex/').substringBefore('?');
        this.uAccount = (id == null) ? new UserAccount__c() :[select First_Name__c,Last_Name__c,Phone__c,Email__c,UserLogo__c,Role__c,Company_Name__c,Location__c from UserAccount__c where Id=: id];        
        this.uEmployeeDetail = (id == null) ? new List<Employee_Detail__c>() : [select First_Name__c,Last_Name__c,Employee_ID__c from Employee_Detail__c where Id=:id];        
        		if (this.uEmployeeDetail.size() ==0)
        {
            addEmployee=new Employee_Detail__c();        	
        	this.uEmployeeDetail.add(addEmployee);
        }       
    } 
    public PageReference checkStep1()
    {
        If(uAccount.First_Name__c==null||uAccount.First_Name__c=='')
            {
                PageReference step1=new PageReference('/apex/RegisterStep1');
                return step1;
            }
        else
        {
        	return null;
        }        
    }
    public PageReference movetoStep1()
    {                
        PageReference nextPage=new PageReference('/apex/RegisterStep1');
        nextPage.setRedirect(false);
        return nextPage;
    }
    public PageReference movetoStep2()
    {
		/*if(userimage.name==''||userimage.name==null)        
        {           
			userimage.body = userlogo; 
			userimage.name = 'userlogo'; 			
			userimage.ContentType = 'application/jpg'; 
        }	*/	
        PageReference nextPage=new PageReference('/apex/SampleLogin');
        nextPage.setRedirect(false);
        return nextPage;
    }
    public PageReference movetoStep3()
    {                
        PageReference nextPage=new PageReference('/apex/RegisterStep3');
        nextPage.setRedirect(false);
        return nextPage;
    }
    public PageReference gotoConfirmation()
    {
    	PageReference nextPage=new PageReference('/apex/Confirmation');
        nextPage.setRedirect(false);
        return nextPage;
    }
	public PageReference mysave() { 
        insert uAccount;
        for(integer i=0;i<uEmployeeDetail.size();i++)
        {
        	uEmployeeDetail[i].UserAutoID__c=this.uAccount.id;
        }
        insert uEmployeeDetail;
        PageReference pv=new pageReference(ApexPages.currentPage().getURL());   
        if (!ApexPages.hasMessages())
        {showReg=false;}   
        else
        {pv.setRedirect(FALSE);}    	     
        return pv; 
    }
       
    public void addRow()
    {
        addEmployee=new Employee_Detail__c();       
        uEmployeeDetail.add(addEmployee);
    }    
        
    public boolean getShowRegistration()
    {     
        return showReg;
    }
        
}

 
Best Answer chosen by Gstart
Sandeep WaliaSandeep Walia
Hi,

This is how the basic structure of your test class will return. 
@isTest
public class UserAccountsExtensionTracker{
	public static testMethod testMethod1(){
		UserAccount__c objUserAccount = new UserAccount__c();
		objUserAccount.First_Name__c = 'test';
		//Add other User Account fields...
		insert objUserAccount;
		//Similarly insert Employee_Detail__c object
		Employee_Detail__c objEmployeeDetail = new Employee_Detail__c();
		objEmployeeDetail.First_Name__c = 'test';
		//Enter other fields
		insert objEmployeeDetail;
		//This is how we set reference in test class
		//Copied from https://developer.salesforce.com/forums/?id=906F0000000BYwzIAG
		Test.startTest();
		PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
		pageRef.getParameters().put('id', String.valueOf(objUserAccount.Id));
		Test.setCurrentPage(pageRef);
		Test.stopTest();
		//Call all the methods in the class to be covered and change logic for special cases
		UserAccountsExtension.checkStep1();
		UserAccountsExtension.movetoStep1();
		//Call all other methods....
	}
}

I have added the logic to cover Pagereference on line 16-18.
I would also like to highlight these below 2 pointers regarding your test class:
  1. Line 11 of your code ( String pgName=ApexPages.currentPage().getUrl().substringAfter('/apex/').substringBefore('?'); ) is  not being used in your class.
  2. The uAccount object should be a list(As it would break your code if no object is found in the query.

Also refer to below best practices for writing test classes in Salesforce
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

http://www.sfdc99.com/2013/11/02/principles-of-a-good-test-class/

http://www.jitendrazaa.com/blog/salesforce/apex/faq-writing-test-class-in-salesforce/


Hpoe it helps,
Sandeep