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
Vinod TomarVinod Tomar 

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

FabianFabian

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:

insert new CustomSetting__c(Field__c = 'Value');

 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)

@isTest(SeeAllData=true)
public static testMethod void myGreatTest() {
    ...
}

 

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

Vinit_KumarVinit_Kumar

Did you use 

 

@IsTest(SeeAllData) in you test class because without it you can't access custom settings record.

abhishek 1996abhishek 1996
just like object create a record.

@isTest(SeeAllData=true)
public static testMethod void myTest() {
    
     CustomSetting__c cs = new CustomSetting__c();
        cs.Name='test';
        //cs.Other fiels values
     insert cs;

}
 
Mukesh Kumar 107Mukesh Kumar 107
1. Custom Settings are not visible in the Test Class by default.
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
 
/****************************************************
        testSetup
    *****************************************************/
    @testSetup public static void setup(){

        // insert all custom setttings which is being used in the Apex Code

        List<TriggerController__c> listTriggerController = New List<TriggerController__c>();

        TriggerController__c customSetTriggerController_Sales_Targets_Schedule_Trigger_1 = new TriggerController__c();
        customSetTriggerController_Sales_Targets_Schedule_Trigger_1.name = 'Sales_Targets_Schedule_Trigger_1';
        customSetTriggerController_Sales_Targets_Schedule_Trigger_1.Is_Active__c = true;
        customSetTriggerController_Sales_Targets_Schedule_Trigger_1.Object_Name__c = 'Sales_Targets_Schedule__c';
        listTriggerController.add(customSetTriggerController_Sales_Targets_Schedule_Trigger_1);

        TriggerController__c customSetTriggerController_ActivateSalesTargetTriggerLegacy = new TriggerController__c();
        customSetTriggerController_ActivateSalesTargetTriggerLegacy.name = 'ActivateSalesTargetTriggerLegacy';
        customSetTriggerController_ActivateSalesTargetTriggerLegacy.Is_Active__c = false;
        customSetTriggerController_ActivateSalesTargetTriggerLegacy.Object_Name__c = 'Sales_Target__c';
        listTriggerController.add(customSetTriggerController_ActivateSalesTargetTriggerLegacy);

        TriggerController__c customSetTriggerController_Sales_Target_Trigger_1 = new TriggerController__c();
        customSetTriggerController_Sales_Target_Trigger_1.name = 'Sales_Target_Trigger_1';
        customSetTriggerController_Sales_Target_Trigger_1.Is_Active__c = false;
        customSetTriggerController_Sales_Target_Trigger_1.Object_Name__c = 'Sales_Target__c';
        listTriggerController.add(customSetTriggerController_Sales_Target_Trigger_1);

        TriggerController__c customSetTriggerController_Sales_Target_Trigger_2 = new TriggerController__c();
        customSetTriggerController_Sales_Target_Trigger_2.name = '    Sales_Target_Trigger_2';
        customSetTriggerController_Sales_Target_Trigger_2.Is_Active__c = false;
        customSetTriggerController_Sales_Target_Trigger_2.Object_Name__c = 'Sales_Target__c';
        listTriggerController.add(customSetTriggerController_Sales_Target_Trigger_2);

        insert listTriggerController;

    }

 
Ashwini deshpandeAshwini deshpande
Refer "https://salesforce.stackexchange.com/questions/9565/how-to-get-custom-settings-in-apex-test-code" to create Custom setting in test class.
Aradhika Chawla 3Aradhika Chawla 3
@Ashwini deshpande Thanks for the answer. Its works perfectly.
Ad CadAd Cad
This is messed up - Custom Meta Data is available for Test code, so why not Custom Settings?
Mike ArthurMike Arthur
@Ad Cad - Custom Metadata is metadata.  It defines the structure of your org, just like the metadata for field definitions and validation rules.
Custom Setting records are data, just like Account records and Contact records.  In test methods, you have to create your own Account and Contact test data, and the same is true for Custom Setting data.