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
aressaress 

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;
        }
    } 
Raj VakatiRaj Vakati
try this code
 
@isTest
private class CountryDependentControllerTest {

     
    static TestMethod void CountryDependentController_UnitTest(){
        Test.startTest();
		Country__c co = new Country__c();
		co.Name ='123123';
		// add other fields 
		insert co ;
		City__c co = new City__c();
		// add other fields 
		co.Name='BLR',
		insert co ;
          CountryDependentController  co = new CountryDependentController (); 
		  co.getCountrylist();
		  co.getCityList();
		  
        Test.stopTest();
    }

      
}

 
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
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
@isTest
private class CountryDependentControllerTest 
{
    static TestMethod void CountryDependentController_UnitTest()
	{

		Country__c CountryObj = new Country__c();
			CountryObj.Name ='INDIA';
			// add other fields 
		insert CountryObj ;
		
		City__c cityObj = new City__c();
			cityObj.Name='Delhi';
			cityObj.Country__c  ='INDIA';
			// add other fields 
		insert cityObj;
        
		Test.startTest();
		
		
          CountryDependentController  Cdc = new CountryDependentController(); 
		  List<SelectOption> lstContry = Cdc.getCountrylist();
		  System.assert(lstContry != null);
		  Cdc.country='INDIA';
		  Cdc.getCityList();
		  
        Test.stopTest();
    }    
}

Let us know if this will help you