28

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.

53

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 '14 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 '14 at 14:43
  • 3
    @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. – Dnyanesh KC May 26 '16 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 – dev.e.loper Aug 21 '17 at 12:41
  • 1
    Value__c is just an example name. Use your real field names. – Shane Jan 6 at 2:33
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.