When building reliable Salesforce applications, test data consistency is just as important as test coverage.
This is where Custom Metadata becomes extremely valuable. Instead of hardcoding values in test classes,
you can use Custom Metadata to store reusable configuration data that works consistently across environments.
In this guide, we will explain how to create Custom Metadata in a Salesforce test class using a simple,
step-by-step approach. We will also show how Provar, a Salesforce automation testing tool, helps teams validate
Custom Metadata usage as part of their automated testing strategy.
What Is Custom Metadata in Salesforce?
Custom Metadata allows you to define reusable configuration data that can be deployed, packaged, and
accessed in Apex code, flows, and test classes. Unlike Custom Settings, Custom Metadata is fully deployable
and does not require manual data setup after deployment.
In simple terms, think of Custom Metadata as a settings table that:
- Stores configuration values instead of business data
- Works across sandboxes and production
- Can be accessed in Apex without SOQL limits
- Is ideal for feature flags, rules, and test configuration
Why Use Custom Metadata in Test Classes?
When writing tests, developers often struggle with:
- Hardcoded values that break when logic changes
- Test data that behaves differently in each environment
- Complex setup logic inside test classes
Custom Metadata solves these issues by allowing you to reference stable configuration data inside your tests.
Custom Metadata vs Custom Settings (Quick Comparison)
| Feature | Custom Metadata | Custom Settings |
|---|---|---|
| Deployable | Yes | No |
| Accessible in Tests | Yes | Yes |
| SOQL Required | No | Yes |
| Best for Configuration | Yes | Limited |
For modern Salesforce development and testing, Custom Metadata is the recommended approach.
How to Create Custom Metadata in Salesforce
Step 1: Create a Custom Metadata Type
Follow these steps:
- Go to Setup
- Search for Custom Metadata Types
- Click New Custom Metadata Type
- Enter:
- Label (example: Test_Config)
- Object Name (Test_Config__mdt)
- Description
Step 2: Create Fields for Configuration
Add fields such as:
- Boolean field (Is_Enabled__c)
- Text field (Environment_Name__c)
- Number field (Max_Records__c)
Step 3: Create Custom Metadata Records
After creating the type, add records:
- Click Manage Records
- Create a new record with test-friendly values
- Save and deploy
How to Access Custom Metadata in a Test Class
You can access Custom Metadata without SOQL by using the getInstance() or getAll() methods.
Example: Accessing Custom Metadata in Apex Test Class
Test_Config__mdt config = Test_Config__mdt.getInstance('Default_Config');
Boolean enabled = config.Is_Enabled__c;
This approach ensures your test logic always uses the same configuration values, regardless of environment.
How to Create Custom Metadata Records Inside Test Classes
Salesforce allows you to create Custom Metadata records dynamically in test context using
Test.loadData() or direct insert (with limitations).
Option 1: Using Test.loadData()
List<Test_Config__mdt> configs =
Test.loadData(Test_Config__mdt.sObjectType, 'TestConfigData');
This method uses a CSV file stored as a static resource and is ideal for large test data sets.
Option 2: Direct Insert (Supported in Tests)
Test_Config__mdt record = new Test_Config__mdt(
DeveloperName = 'Test_Config',
Is_Enabled__c = true
);
insert record;
This is useful for quick unit tests that need only one or two records.
Best Practices for Using Custom Metadata in Test Classes
- Keep metadata records simple and descriptive
- Use one metadata record per configuration scenario
- Avoid hardcoding business logic in tests
- Version-control metadata with source-driven development
- Align metadata values with your test strategy
Testing Custom Metadata with Provar
When you test Salesforce applications using Provar,
Custom Metadata becomes even more powerful. Provar allows you to:
- Validate metadata-driven logic across environments
- Reuse test data without manual reconfiguration
- Automate configuration-driven scenarios
- Run metadata-based tests in End-to-End testing
This ensures your tests remain stable even when business rules change.
Using Custom Metadata in CI/CD Pipelines
Custom Metadata fits perfectly into modern DevOps workflows. Because metadata records are deployable,
they can move automatically through environments using version control and
This means your tests and configuration remain aligned from development to production.
Common Mistakes to Avoid
- Using Custom Metadata for transactional data
- Hardcoding metadata record names without constants
- Creating too many metadata types
- Not testing metadata-driven logic
Conclusion: Build Better Tests with Custom Metadata and Provar
Using Custom Metadata in Salesforce test classes is one of the most effective ways to create
reliable, maintainable, and scalable tests. It removes hardcoded values, improves environment consistency,
and simplifies test setup.
With Provar, teams can automate Salesforce testing using metadata-driven logic, validate configurations
across environments, and integrate tests seamlessly into CI/CD pipelines. This combination ensures your
Salesforce automation is not only robust but future-proof.
If you want to improve your Salesforce test strategy, Custom Metadata paired with Provar is the smartest place to start.
read more