[SalesForce] Custom Setting returns null in apex test

In a visual flow that is autolaunched on Opportunity creation. In it I have a lookup element to a custom setting that returns a string based on a date value.

When I run this in the UI and create an Opp everything works fine, but when I create an Opp while running an apex test class the custom setting lookup always returns null. Is there a reason this doesn't work in an apex test class or it is just a bug?

Another answer suggested rebuilding the custom settings records, which I did with no luck.

Best Answer

Custom Settings are treated as data, not metadata, in test context, and as such are not visible during the test run.

If you need that setting available during the course of your unit test (in Apex or Flow), the test or its associated @testSetup method needs to create and insert the Custom Setting value.

Related Topic