Salesforce Dictionary - Free Salesforce GlossarySalesforce Dictionary
Salesforce QA / Tester
medium

How do you test code that depends on shared org-wide data (e.g., user settings, custom metadata)?

Some code depends on data that exists outside test isolation: User records, Profiles, Custom Metadata.

User records:

  • Tests can create test users.
  • System.runAs(user) to test under that user.

Profiles:

  • Profile records EXIST in test context (not isolated).
  • Reference profile by name: [SELECT Id FROM Profile WHERE Name = 'Standard User'].

Custom Metadata Type:

  • CMDT records ARE visible in tests (treated like metadata, not data).
  • No need to create in tests.

Custom Settings:

  • Custom Setting records NOT visible in tests by default.
  • Create in @TestSetup if needed.

Custom Permissions:

  • Visible in tests (metadata).

Picklist values:

  • Defined in metadata; visible in tests.

Test patterns:

@TestSetup
static void setup() {
    // For Custom Settings:
    My_Settings__c s = new My_Settings__c(Threshold__c=100);
    insert s;
}

@isTest
static void testWithCMDT() {
    // CMDT visible without setup
    My_Type__mdt config = My_Type__mdt.getInstance('Default');
    System.assertNotEquals(null, config);
}

Common pitfalls:

  • Forgetting Custom Settings need explicit setup.
  • Hardcoded Profile IDs (varies per org).
  • Assuming all metadata visible (some isn't).

Senior insight: understand what's visible vs not in tests. Patterns vary.

Why this answer works

Senior. The visibility distinctions are mature.

Follow-ups to expect

Related dictionary terms