Salesforce Developers put out the blog post Testing Custom Metadata Types that includes the section Testing Global Custom Metadata.
...add an extra field to your custom metadata type to indicate what sort of tests the type is active for. For example, you could add a Text field called TestCase__c to your type. You can leave this field blank for production custom metadata and set it for test custom metadata.
Class with static setting to toggle records to retrieve.
public class TestContext {
public static String testCase {public get; public set;}
}
Queries against the custom metadata type use the static setting to retrieve testing configuration.
OurCustomMetadata__mdt[] rows =
[SELECT QualifiedApiName, Field1__c
FROM OurCustomMetadata__mdt
WHERE TestCase__c = :TestContext.testCase];
Test indicates which configuration to use.
public static testmethod void testWelcomePageWorksWithTestCase1() {
TestContext.testCase = 'TestCase1';
/* test the Welcome page */
}
Personally I find the proposed solution a bit ugly and have voted for Custom MetaData Types need APEX methods to access/update.
See also: