0

how to cover custom setting in test class and how to get getInstance(UserInfo.getProfileId())

this is my class code

Profile_Oons__c contentesetup = Profile_Oons__c.getInstance(UserInfo.getProfileId());
Vipin Indora
  • 898
  • 5
  • 18
  • 38

2 Answers2

5

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

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.

developer__c
  • 1,307
  • 1
  • 15
  • 36
  • my question is how to get from query on customsettings – Vipin Indora Jun 14 '16 at 10:15
  • You should insert the expected value, test and assert. Then insert a bad value then retest and assert. This means your test will run successfully if your code is deployed to a SalesForce org where you have not configured the custom setting. – developer__c Jun 15 '16 at 12:10
0

You can use below link to do query on custom setting:

Retrieving Values from Custom Settings

If you need to create custom setting in Test class, then I am much satisfy with the @Developer__c comments.

Hope it resolves your problem.

Anurag Bhardwaj
  • 896
  • 6
  • 15