+ Start a Discussion
shweta kumari 25shweta kumari 25 

custom settings in text classes

I have countries in custom settings and corresponding states in another custom setting.
I have a working code which will fetch the countries in picklist and then based on countries selection it will populatate the states picklist 

I need to write test class for the same; not sure how to go about it to cover the custom settings.(test cases)

Please let me know how we can achieve this

thanks
shweta
Best Answer chosen by shweta kumari 25
Raj VakatiRaj Vakati
I will not sugggest to use  SeeAllData=true .. 

Custom settings are nothing but an object .All you need in Test Class is instantiate the custom settings object and create the records of the custom settings and insert it in Test class itself.Make sure you bulkify your insert call.


 
list<SFA_ContactFields__c> lstContacts=new list<SFA_ContactFields__c>();//bulk List of custom setting object for bulk insert

SFA_ContactFields__c csContactFields=new SFA_ContactFields__c(); //Custom Setting for Contact Fields
csContactFields.Name=’CreatedDate’;//Static record 1 of custom setting
lstContacts.add(csContactFields);

SFA_ContactFields__c csContactFields1=new SFA_ContactFields__c();
csContactFields1.name=’IsDeleted’;//Static Record 2 of custom settings
lstContacts.add(csContactFields1);

insert lstContacts;


 

All Answers

Steven NsubugaSteven Nsubuga
Hi Shweta, you can either use @isTest(SeeAllData=true) to be able to access existing custom settings or you can create test settings data for use in your test class.

For example
CustomSetting__c cs = new CustomSetting__c();
cs.Name='test';
cs.Is_Active__c = false;
insert cs;


 
Raj VakatiRaj Vakati
I will not sugggest to use  SeeAllData=true .. 

Custom settings are nothing but an object .All you need in Test Class is instantiate the custom settings object and create the records of the custom settings and insert it in Test class itself.Make sure you bulkify your insert call.


 
list<SFA_ContactFields__c> lstContacts=new list<SFA_ContactFields__c>();//bulk List of custom setting object for bulk insert

SFA_ContactFields__c csContactFields=new SFA_ContactFields__c(); //Custom Setting for Contact Fields
csContactFields.Name=’CreatedDate’;//Static record 1 of custom setting
lstContacts.add(csContactFields);

SFA_ContactFields__c csContactFields1=new SFA_ContactFields__c();
csContactFields1.name=’IsDeleted’;//Static Record 2 of custom settings
lstContacts.add(csContactFields1);

insert lstContacts;


 
This was selected as the best answer
shweta kumari 25shweta kumari 25
Hi Raj
In test class, if we use custom settings and dont specify seealldate=true does it work?

shweta
 
devedeve
Yes if we insert that custom setting in testclass.