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
@TestSetupif needed.
Custom Permissions:
- Visible in tests (metadata).
Picklist values:
- Defined in metadata; visible in tests.
Test patterns:
`apex @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.
