You need to sign in to do that
Don't have an account?
Gstart
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; } }
This is how the basic structure of your test class will return.
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:
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