1

I am trying to understand if I can make use of Custom Metadata Type record by quering it in a HttpCalloutMock this way:

public HTTPResponse respond(HTTPRequest request) {
  String myString = [Select Value__c 
                     From PrimaryMetadata__mdt 
                     Where DeveloperName = 'DefaultValue'].Value__c;
  response = new HttpResponse();
  response.setHeader('Content-Type', 'application/json');
  response.setBody(myString);
  response.setStatusCode(200);
  return response;
} 

I am implementing this in a Developer sandbox, so would like to understand the implications of it while deploying into other orgs.

2
  • Why would you not be able to? Seems like a strange question to ask.
    – Adrian Larson
    Commented Aug 9, 2019 at 15:45
  • :) Just making sure. My only concern is: If I do it, would I get any deployment errors while deploying the mock class, test class and Metadata type record in a single package? Commented Aug 9, 2019 at 15:46

1 Answer 1

2

Yes, you can. Custom Metadata is considered, well, metadata, and it's available to query in an Apex unit test (unlike Custom Settings).

It's important to bear in mind that this creates an external dependency of your unit tests. Custom Metadata records can be modified by users with permission to customize the application, and this could cause Apex unit test failures if your tests are dependent on specific values (or non-blank values, for example).

You'll have to ensure that the relevant Custom Metadata records are deployed between environments alongside your code, or your tests will fail immediately.

6
  • One question: While deploying the custom metadata record, test class and the mock class for the first time (in a single change set), will I get any test class failure errors? As the custom metadata is not on the source org by then, will it cause this sort of failure? Commented Aug 9, 2019 at 15:54
  • 2
    @AustinEvans No. Just make sure they're all deployed together.
    – David Reed
    Commented Aug 9, 2019 at 15:55
  • Awesome!! These are small things, but could cause a catastrophe in the deployments. Commented Aug 9, 2019 at 16:01
  • @AustinEvans : It's important to bear in mind that this creates an external dependency of your unit tests. Custom Metadata records can be modified by users with permission to customize the application, and this could cause Apex unit test failures if your tests are dependent on specific values (or non-blank values, for example). This is why I use selector classes for custom metadata and ApexMocks to ensure my testcode can control/isolate the __mdt values.
    – cropredy
    Commented Aug 9, 2019 at 17:42
  • Hey @cropredy, can you please post any sample ( or prototype) code snippet for: selector classes for custom metadata and ApexMocks, so that I can learn something from it? I really appreciate any help. Commented Aug 9, 2019 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .