You need to sign in to do that
Don't have an account?

test class for custom setting
Hello Experts,
Need test class for following VF page that display a dependent picklist which is a customSetting. Consider all positive negative test cases
--------------------------VF PAGE-----------------------------
<apex:page controller="CountryDependentController" showHeader="true" sidebar="false">
<apex:pageBlock >
<apex:form >
<!-- actionFunction -->
<apex:actionFunction action="{!getCountrylist}" name="rerenderCities" rerender="Cities" >
<apex:param name="firstParam" assignTo="{!country}" value="" />
</apex:actionFunction>
<!-- SelectList-->
Country : <apex:selectList value="{!country}" size="1" onchange="rerenderCities(this.value)">
<apex:selectOptions value="{!CountryList}"></apex:selectOptions>
</apex:selectList> <br/> <br/>
City : <apex:selectList id="Cities" value="{! cities}" size="1">
<apex:selectOptions value="{!CityList}"></apex:selectOptions>
</apex:selectList>
</apex:form>
</apex:pageBlock>
</apex:page>
------------------------------------CONtroller--------------------------------------------------
public class CountryDependentController {
Map<String, Country__c> countryMap = Country__c.getall();
public String cities{ get; set; }
public String country{ get; set;}
public List<SelectOption> getCountrylist() {
/* Method will give list of countries
* @param: NA
* @return: selectoption: The country which will be selected by user will be returned
*/
List<SelectOption> selectoptionList = new List<SelectOption>();
for(String country : countryMap.keySet()){
selectoptionList.add(new selectoption(country,country));
}
return selectoptionList;
}
/* Method will give list of cities
* @param: NA
* @return: selectoptionList: Based on selected country user can select any city related to that country.
*/
public List<SelectOption> getCityList() {
List<SelectOption> selectoptionList = new List<SelectOption>();
if( country == null ) {
return selectoptionList;
} else {
List<City__c> cityList = City__c.getAll().values();
for(City__c city : cityList) {
if(city.Country__c == country ) {
selectoptionList.add(new selectoption(city.Name, city.Name));
}
}
}
return selectoptionList;
}
}
Need test class for following VF page that display a dependent picklist which is a customSetting. Consider all positive negative test cases
--------------------------VF PAGE-----------------------------
<apex:page controller="CountryDependentController" showHeader="true" sidebar="false">
<apex:pageBlock >
<apex:form >
<!-- actionFunction -->
<apex:actionFunction action="{!getCountrylist}" name="rerenderCities" rerender="Cities" >
<apex:param name="firstParam" assignTo="{!country}" value="" />
</apex:actionFunction>
<!-- SelectList-->
Country : <apex:selectList value="{!country}" size="1" onchange="rerenderCities(this.value)">
<apex:selectOptions value="{!CountryList}"></apex:selectOptions>
</apex:selectList> <br/> <br/>
City : <apex:selectList id="Cities" value="{! cities}" size="1">
<apex:selectOptions value="{!CityList}"></apex:selectOptions>
</apex:selectList>
</apex:form>
</apex:pageBlock>
</apex:page>
------------------------------------CONtroller--------------------------------------------------
public class CountryDependentController {
Map<String, Country__c> countryMap = Country__c.getall();
public String cities{ get; set; }
public String country{ get; set;}
public List<SelectOption> getCountrylist() {
/* Method will give list of countries
* @param: NA
* @return: selectoption: The country which will be selected by user will be returned
*/
List<SelectOption> selectoptionList = new List<SelectOption>();
for(String country : countryMap.keySet()){
selectoptionList.add(new selectoption(country,country));
}
return selectoptionList;
}
/* Method will give list of cities
* @param: NA
* @return: selectoptionList: Based on selected country user can select any city related to that country.
*/
public List<SelectOption> getCityList() {
List<SelectOption> selectoptionList = new List<SelectOption>();
if( country == null ) {
return selectoptionList;
} else {
List<City__c> cityList = City__c.getAll().values();
for(City__c city : cityList) {
if(city.Country__c == country ) {
selectoptionList.add(new selectoption(city.Name, city.Name));
}
}
}
return selectoptionList;
}
}
1) https://trailhead.salesforce.com/modules/apex_testing
Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm
Sample Test Class for you
Try Test Class like below
Let us know if this will help you