In my Apex code, I already have an external HTTP callout.
Now, I aim to use a boolean flag in production code to control whether do real HTTP callout or read a static JSON from mdt records (i.e. a human can update the mockup data in mdm record to mimic the returning data from the external API).
The mockup works as follows: instead of making a real callout, the Apex can read a static JSON value stored in a custom metadata type record. I toggle the mockup on/off with a boolean flag in a custom setting.
In the production code:
if (useMockup()) { // check the boolean flag in custom setting;
return getMockUpHttpResponse(); // the data is retrieved from custom metadata type record;
} else{
// do real http callout;
}
My challenge is how to unit test this logic. Custom metadata types are exposed to Apex code, and the record in the org determines whether the unit test succeeds or fails, which conflicts with the concept of unit testing.
Thanks for your help!