You need to sign in to do that
Don't have an account?
Custom Setting values in Test Class
Hi,
I am accessing custom setting values in my apex code and its working fine. When I am calling it through test class then No values gets in from Custom Setting. It always blank in test class. Can somebody help me to understand why custom settings values are not accessible in test class.
Thanks,
Vinod Kumar
Hey there,
Custom Settings are "Data" and as such are not part of the test-environment as of Spring '12.
Just like every object will have no records by default.
What you can do is either initially load the custom setting by using:
Or you could add the following annotation to your testmethod: (see http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm)
Beware that the latter solution will probably fail if you deploy this very testclass using a changeset that also creates the custom setting (as there really is no data yet).
Regards,
Fabian
Did you use
@IsTest(SeeAllData) in you test class because without it you can't access custom settings record.
@isTest(SeeAllData=true)
public static testMethod void myTest() {
CustomSetting__c cs = new CustomSetting__c();
cs.Name='test';
//cs.Other fiels values
insert cs;
}
2. Method-1 : You can use @istest(SeeAllData=true)
3. Method2-: You can create custom setting required for the test in the test method
4.Note that if you create custom setting through code within test method then you will not be able to use (SeeAllData=true)
Here is the code sample to create custom setting data in test class