I have some code which relies on some Custom Metadata records to exist, but I don't want my tests to depend on real records in the org. How can I either inject or mock custom metadata records into the class?
Here's a psudocode example of what the code is doing:
global static String fetchCalloutPath(String endpointName){
endpoint__mdt endpointMDT = endpoint__mdt.getInstance(endpointName);
if(endpointMDT == null){
throw new NoEndpointMetadataFoundException('Unable to find a endpoint__mdt record for endpoint "' + endpointName + '"');
}
Credential_Map__mdt credentialMDT = getCredential(endpointMDT.APIName__c);
String calloutPath = 'callout:' + credentialMDT.Named_Credential__c + endpointMDT.Value__c;
return calloutPath;
}
private static Credential_Map__mdt getCredential(String apiName){
return [
SELECT Named_Credential__c
FROM Credential_Map__mdt
WHERE APIName__c = :apiName
];
}
So in order to get a Credential_Map__mdt record I must first get an endpoint__mdt record.
I want to be able to reliably test and therefore mock or inject the metadata records, but I'm having a hard time understanding how best to achieve this, especially with there being a getInstance call involved. I'm thinking I might have to change that to a SOQL query in order to actually get this working but I'm hoping someone might have a solution for this (if so I can use this method elsewhere in the org). Also if I have to create interfaces or mock classes, will I need 2 due to the different metadata types involved? I'm hoping there might be some generic solution I could use elsewhere in the org.
Hoping for some guidance. Thanks!
Object
that you would need to cast). salesforce.stackexchange.com/questions/385299/… . (The question would be closed if I were to suggest this question as a duplicate of that one).