Sub topic 8
Testing code that reads custom metadata
This chapter assumes you can write Apex classes and @IsTest classes, and that you have already defined a custom metadata type and read its records with getInstance(). It covers how to test code that depends on those records.
Why testing is different
Custom metadata records are not like custom object records. Apex tests can see them without @IsTest(SeeAllData=true), but a test cannot create them with DML. A test that depends on a record therefore depends on whatever records exist in the org where it runs.
That causes two problems:
- A test passes in your sandbox and fails in a fresh scratch org, because the record was never deployed there.
- Someone changes a record's value later and an unrelated test breaks.
The fix is to put a seam between your logic and the metadata, so a test can supply the value directly.
Add a test override
Keep all metadata access in one small class. Add a @TestVisible static map that the class checks before it reads the record.
public with sharing class FeatureFlags {
@TestVisible
private static Map<String, Boolean> testOverrides = new Map<String, Boolean>();
public static Boolean isEnabled(String flagName) {
if (testOverrides.containsKey(flagName)) {
return testOverrides.get(flagName);
}
Feature_Flag__mdt flag = Feature_Flag__mdt.getInstance(flagName);
return flag != null && flag.Is_Enabled__c;
}
}@TestVisible lets test classes read and write the private field. Production code has no reason to touch it. Static state resets between test methods, so one test's override never leaks into another.
Write the tests
@IsTest
private class FeatureFlagsTest {
@IsTest
static void overrideControlsFlagValue() {
FeatureFlags.testOverrides.put('New_Checkout', true);
System.assertEquals(true, FeatureFlags.isEnabled('New_Checkout'));
}
@IsTest
static void missingFlagIsDisabled() {
System.assertEquals(false, FeatureFlags.isEnabled('Does_Not_Exist'));
}
}The first test controls the result completely, so it passes in any org. The second test checks the fallback behavior. getInstance() returns null for an unknown name, and the class turns that into false. This test needs no records at all, because the name is one that should never exist.
Test the code that uses the flag
The override matters most when you test a class that branches on a flag. Each test sets the flag it needs, then checks one branch.
public with sharing class CheckoutService {
public static String start() {
return FeatureFlags.isEnabled('New_Checkout') ? 'new' : 'legacy';
}
}
@IsTest
private class CheckoutServiceTest {
@IsTest
static void usesNewFlowWhenEnabled() {
FeatureFlags.testOverrides.put('New_Checkout', true);
System.assertEquals('new', CheckoutService.start());
}
@IsTest
static void usesLegacyFlowWhenDisabled() {
FeatureFlags.testOverrides.put('New_Checkout', false);
System.assertEquals('legacy', CheckoutService.start());
}
}Both branches are covered, and neither test depends on the deployed record.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Inserting a `Feature_Flag__mdt` record in a test | The test fails, because DML on custom metadata is not allowed | Use an override map |
| Asserting against the real record's value | The test breaks when an admin edits the record | Override the value, or assert only the fallback |
| Overriding inside the production method only | Tests cannot reach the map | Mark the field `@TestVisible` |
| Never testing the missing-record case | A `null` dereference reaches production | Add a test with a name that does not exist |
Exercise
Exercise
A test calls FeatureFlags.isEnabled('New_Checkout') with no override and asserts true. It passes in your sandbox but fails in a new scratch org. Why, and how do you fix it?
Show answer
The record New_Checkout was deployed to the sandbox but not to the scratch org, so getInstance() returns null and isEnabled returns false. The test depends on org data. Fix it by setting FeatureFlags.testOverrides.put('New_Checkout', true) before the assertion, so the result no longer depends on which records exist.
Exercise
Why can't a test class create a Feature_Flag__mdt record with insert to set up its data?
Show answer
Apex does not support DML on custom metadata records. They are changed by deployment, not by insert, update, or delete, so tests must supply values through an override such as a @TestVisible map.