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
salesforcemicky125salesforcemicky125 

How to write a test case for a Custom controller in salesforce

Hi, Can anyone help me out on writing a test case for a custom controller Below is the Controller class public class CreateContactController { public PageReference redirectPopup() { try { emailc=con.Email; set recids = new set(); set campusrecid = new set(); List RecordTypeids = new List(); RecordTypeids =[select id,name from RecordType where sobjecttype = 'Contact' and name='campus' limit 1]; con.recordtypeid=RecordTypeids[0].id; insert con; con=new Contact (); objEvent= new Event_Registration__c (); objContact =[select id,name,FirstName,LastName,MobilePhone,Email from Contact where email= :emailc limit 1]; objEvent.Contact__c=ObjContact[0].id; system.debug('bugcontact'+ObjContact[0].id); ObjEvent.Recruiting_Event__c=objRecEvent[0].id; ObjEvent.Attended__c=True; insert objEvent; PageReference pr=new PageReference('/apex/eventRegistration/checkin'); pr.setRedirect(true); return pr; } catch(Exception e){ ApexPages.addMessages(e); return null; } } public PageReference closePopup() { displayPopup = false; return null; } public boolean displayPopUp { get; set; } public PageReference showPopup() { displayPopup = true; return null; } public String getDisplaypblock() { return null; } public string schoolname{get;set;} public string emailc{get;set;} public Contact con{get; set;} public List objContact {get;set;} public Event_Registration__c ObjEvent{get;set;} public List objRecEvent{get;set;} public Recruiting_Event__c ObjAccountRec{get;set;} public Boolean displaySearchResults{get;set;} public String selectedValue {get { if(selectedValue <> null && selectedValue <> '') { return selectedValue ; } else{ selectedValue = ''; return selectedValue ; } } set;} public List acids = new List(); public List recevents= new List(); public List school = new List(); public CreateContactController(ApexPages.StandardController controller) { emailc=con.Email; } public PageReference Cancel() { return null; } public String getschoollist() { return schoolname; } public CreateContactController() { con=new Contact (); ObjAccountRec = new Recruiting_Event__c(); } public List schoollist { get { recevents= [Select Account__c,Name From Recruiting_Event__c]; schoollist = new List(); for(Recruiting_Event__c temp : recevents) { acids.add(temp.Account__c); } school=[Select id,Name from Account where id in :acids]; for(Account temp1: school) { schoollist.add(new SelectOption(temp1.Id,temp1.Name)); } system.debug('shcooltypename1'+schoolname); return schoollist; } set; } public PageReference incrementCounter() { schoolname=getschoollist(); displaySearchResults=true; date Next = system.today()+1; date Last = system.today()-2; objRecEvent=[SELECT id,name,Location__c,Season__c,Account__c,Room__c,Status__c,Start_Date__c,End_Date__c FROM Recruiting_Event__c WHERE Account__c =:selectedValue and Status__c ='In Progress']; system.debug('++y'); return null; } public pagereference Save() { emailc=con.Email; insert con; con=new Contact (); objEvent= new Event_Registration__c (); objContact =[select id,name,FirstName,LastName,MobilePhone,Email from Contact where email= :emailc limit 1]; objEvent.Contact__c=ObjContact[0].id; system.debug('bugcontact'+ObjContact[0].id); ObjEvent.Recruiting_Event__c='a0GW00000018WpRMAU'; ObjEvent.Attended__c=True; insert objEvent; PageReference pr=new PageReference('/apex/eventRegistration/checkin'); pr.setRedirect(true); return pr; } }
Subhash GarhwalSubhash Garhwal

hI,

 

For Test class, Go for this link , it will definetly help you

 

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

 

Thanks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you