+ Start a Discussion
Ventzi IankoulovVentzi Iankoulov 

Custom Setting’s getInstance() method doesn't return correct value if triggered by test class.

Hello guys,
I’m working on my first ever apex class and currently facing the following issue.
In the class, which I wrote I use getInstance() method as follow:

Id mdrQueueId = (Id)User_Id__c.getInstance().get('mdr_queue_id__c');

to query Custom Settings and get the Id of a queue, which I use later on to update a Case Owner.

When I test the relevant functionality via GUI (as SysAdmin & regular user), everything is working perfect. The query returns the correct queue Id and the Case owner gets updated.

But, if I run the relevant test class, it fails!
I saw that the reason for this is that in case of test class execution the same query returns null instead of the actual Id.
I ran the test both as SysAdmin and as regular user. In both cases I get mdrQueueId = null.
I get the same result (null) if I use the query directly in (another) test class.

Please can you help me understand what causes such different behaviour?
Is there any principle difference by the use of getInstance() method in regular apex classes and test classes?

Thank you in advance!

Ventzi
 

Best Answer chosen by Ventzi Iankoulov
mukesh guptamukesh gupta
Hi Ventzi,

in the test class you need to create cuetom seeting

just like :
custSettings__c setting= new custSettings__c(Name=’Test Name’, Field__c=’Some Value’);
insert setting;
and don't use @isTest(See All Data = 'true'),

if you have any problem then share your test class code.

If this solution is usefull for you, Please mark as a Best Answer to help others.


Regards
Mukesh


 

All Answers

mukesh guptamukesh gupta
Hi Ventzi,

in the test class you need to create cuetom seeting

just like :
custSettings__c setting= new custSettings__c(Name=’Test Name’, Field__c=’Some Value’);
insert setting;
and don't use @isTest(See All Data = 'true'),

if you have any problem then share your test class code.

If this solution is usefull for you, Please mark as a Best Answer to help others.


Regards
Mukesh


 
This was selected as the best answer
Ventzi IankoulovVentzi Iankoulov
Hi Mukesh,

thank you very much for the hint! I was really helpful.

Thanks,
Ventzi
mukesh guptamukesh gupta
Hi Ventiz,

your most welcome.

if you need any help please let me know

Regards
Mukesh