+ Start a Discussion
Bablu Kumar PanditBablu Kumar Pandit 

How to Access Custom Setting In test Class

In Test class When we Insert Custom setting Value then It Does Not Cover But When we use seeAllData = true Then It Cover
use seeAllData =true is not Best Practice so How we Do
Please AnyOne help
ShirishaShirisha (Salesforce Developers) 
Hi Bablu,


You can insert a new Custom Setting record, in your test context like you would normally do with an SObject.

Please find the sample code below:

Whatever_custom_setting__c setting = new Whatever_custom_setting__c();
setting.Name = 'Test Setting';
setting.Value__c = 'Whatever';
insert setting;

and then your function should return the newly created test setting.

Please try the above sample code and let me know,in case of any issues.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Agustin BAgustin B
HI Bablu, you should have a method to create custom settings and call it at the beginning of your test class.
Something like:
public static void createGeneralSettings(){
        GeneralSettings__c generalSettings = new GeneralSettings__c(
            Name= ''Test custom setting');
        insert generalSettings;

    private static void testCustomSetting(){
        //do something


If you are using it in several classes the best would be to create a TestDataFactory.
If it helps please mark as correct, it may help others.
Bablu Kumar PanditBablu Kumar Pandit

Hii  @Shirisha ,

I am Insert New Custom setting Recode In Our Test Class Like as inserting New Recode In sObject But  not cover ,
is there way to cover without using seeAllData = true
Bablu Kumar PanditBablu Kumar Pandit
Hii Agustin,

I am follow your steps but not get solution
Please help me
Agustin BAgustin B

HI Bablu, post the code to see if i can help (my code was an example do not copy exactly the same).

Please like the comment if you are making progress.

Agustin BAgustin B
Hi Bablu, please mark my first answer above as correct so this question can be closed.
Good luck