You need to sign in to do that
Don't have an account?
Nishchal Vashisht 1
How to write Test class for controller Of VF page ?
This is my VF page.
<apex:page Controller="SaleController" > <apex:form > <apex:pageBlock title="country list"> <apex:pageBlockSection columns="3"> <apex:outputLabel > Countries : </apex:outputLabel> <apex:selectList size="1" value="{!selectedCountry}" > <apex:selectOptions value="{!country }"/> <apex:actionSupport event="onchange" action="{!check}" reRender="reg"/> </apex:selectList> <br/> <apex:outputLabel value="Region"/> <apex:selectList id="reg" size="1" value="{!selectedRegion}" > <apex:selectOptions value="{!region }"/> <apex:actionSupport event="onchange" action="{!check}" reRender="stat"/> </apex:selectList> <br/> <apex:outputLabel value="State"/> <apex:selectList id="stat" size="1" value="{!selectedState}" > <apex:selectOptions value="{!state }"/> <apex:actionSupport event="onchange" action="{!check}" reRender="aaa"/> </apex:selectList> <br/> </apex:pageBlockSection> <apex:pageBlockButtons id="aaa" location="bottom" > <apex:commandButton value="Find" action="{!findRecord}" reRender="tab"></apex:commandButton> </apex:pageBlockButtons> </apex:pageBlock> </apex:form> <apex:pageBlock title="Account Record"> <apex:pageBlockTable id="tab" value="{!listOfAc.contacts}" var="contact" > <apex:column value="{!listOfAc.Name}"/> <apex:column value="{!contact.FirstName}"/> <apex:column value="{!contact.LastName}"/> <apex:column value="{!contact.Email}"/> <apex:column value="{!contact.Phone}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page>And this is my Controller.
public class SaleController { public List<SelectOption> country {get;set;} public List<SelectOption> region {get;set;} public List<SelectOption> state {get;set;} public String selectedCountry; public String selectedRegion; public String selectedState; public String tempCountry; public String tempRegion; public String tempState; public Account listOfAc {get;set;} public List<Contact> listOfContact {get;set;} Map<String,List<String>> countryWithState; public SaleController(){ country=new List<SelectOption>(); region = new List<SelectOption>(); state =new List<SelectOption>(); state.add(new SelectOption('','--None--')); country.add(new SelectOption('','--None--')); country.add(new SelectOption('India','India')); country.add(new SelectOption('China','China')); country.add(new SelectOption('US','US')); countryWithState = new Map<String,List<String>>(); countryWithState.put('IndiaNorth',new List<String>{'Delhi','Jammu','Himachal'}); countryWithState.put('IndiaSouth',new List<String>{'Kerala','Karnataka'}); countryWithState.put('IndiaWest',new List<String>{'Sikkim','Manipur','Meghalya'}); countryWithState.put('ChinaNorth',new List<String>{'Hohot','jilin'}); countryWithState.put('ChinaSouth',new List<String>{'yunan','hainan'}); countryWithState.put('ChinaWest',new List<String>{'Zehenjang','shanghai'}); countryWithState.put('USNorth',new List<String>{'New York','Washington'}); countryWithState.put('USSouth',new List<String>{'Texas','Arizona'}); countryWithState.put('USWest',new List<String>{'Virginia','ohio'}); } public String getSelectedCountry(){ return selectedCountry; } public void setSelectedCountry(String s){ tempCountry = s; region.add(new SelectOption('','--None--')); region.add(new SelectOption('North','North')); region.add(new SelectOption('South','South')); region.add(new SelectOption('West','West')); } public String getSelectedRegion(){ return selectedRegion; } public void setSelectedRegion(String s){ tempRegion=s; if(s!=null && this.tempCountry!=null){ for(String loopString:this.countryWithState.get(this.tempCountry+s)){ state.add(new SelectOption(loopString,loopString)); } } } public String getSelectedState(){ return selectedState; } public void setSelectedState(String s){ tempState=s; } public String check(){ return selectedCountry; } public pageReference findRecord(){ List<Id> listOfId =new List<Id>(); if(tempCountry!=null && tempRegion!=null &&tempState!=null){ for(Sales_Location__c tempSale:[select Account__c from Sales_Location__c where Country__c=:tempCountry and State__c=:tempState and Region__c=:tempRegion]) listOfId.add(tempSale.Account__c); listOfAc = [Select Name,(Select FirstName,LastName,Phone,Email from Contacts) from Account where Id In :listOfId limit 1]; }else{ System.debug('All thing is Null'); } return null; } }
Please try the below test class:
Thanks,
Maharajan.C