I have a custom setting used by a visual force page to load error messages based on a code value. In my test code for the page I have this statement

Integer howMany = Message__c.getAll().values().size();

I am calling the getall method to get the values one time from the custom setting and trying to find the size. It is returning me null which means that there is some limitation in using custom settings on a test method. Could some body tell me whether we can load all the values from custom settings on a test method? The scope of the custom setting is protected.

2 Answers 2


Just like normal SObjects, your Test Context doesn't have access to the custom setting records already in the database.

Preferred solution

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

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.

Alternative solution

declare your method with the @isTest(SeeAllData=true) that way your testmethod has visility over the data in your database, outside of the test context. However, these testmethods might fail in cases where there is no data in the custom setting.

  • I have placed the code in my testclass again it is showing attempt to dereference null
    – Sunny
    Jan 3, 2014 at 12:16
  • I experienced the same issue and the SeeAllData=true solved it indeed. Just a note If the testmethod in the class not in a separate test class it works as well. Probably it sees all the data in that case when we're writing test method to the same class where the business logic is.
    – tamastom
    Jan 28, 2014 at 14:43
  • 4
    @tamastom @Sunny although your problem is solved, it is not recommended to use the SeeAllData=true annotation while writing test code as it makes your tests dependent on the existing data. It will be preferable to create the test data as @pjcarly mentioned above. There seems to be no error in the code they provided. Please go through your code once again to find the root cause of the exception. May 26, 2016 at 6:47
  • I cannot set Value__c on a setting object. However, I was able to do the following salesforce.stackexchange.com/a/189328/34947 Aug 21, 2017 at 12:41
  • 2
    Value__c is just an example name. Use your real field names.
    – Shanerk
    Jan 6, 2019 at 2:33

The following worked for me.

The code that uses a token:

        Map<String, ServiceTokens__c> setting = ServiceTokens__c.getall();
        ServiceTokens__c token = setting.get('Token__c');                              
        String token = token.Token__c;

In the test setup method I have the following:

        ServiceTokens__c tokenCustomSetting = new ServiceTokens__c();         
        tokenCustomSetting.Name = 'Token__c';        
        tokenCustomSetting.Token__c = 'placholder';        
        insert tokenCustomSetting;

In the above code, the Name is the API Name that you see in your Custom Settings. Here is screenshot of my custom setting:

enter image description here

  • 1
    +1 for the snapshot which helped relate the code perfectly. May 5, 2020 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .