Sub topic 15
Testing field access with System.runAs
This chapter assumes you know user-mode queries and DML in Apex, permission sets, and basic Apex test structure. You will write tests that prove your code respects field-level security (FLS).
Why tests need System.runAs
Apex tests run in system mode as the user who starts the test run. That user is usually an administrator, so every field is readable and editable. A test that queries a restricted field passes even when real users would be blocked. System.runAs(user) runs a block of code with that user's permissions, sharing, and FLS.
The testing flow
- Create a test user on a profile with minimal access.
- Assign a permission set that grants or withholds the field.
- Run the code under test inside
System.runAs. - Assert on the result, either a thrown exception or the data returned.
Testing the blocked case
This test checks that a user-mode query fails when the user cannot read a field. It assumes the custom field Internal_Notes__c exists on Account and that the Standard User profile cannot read it.
@IsTest
private class AccountFlsTest {
@IsTest
static void userModeQueryFailsWithoutFieldAccess() {
Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
User u = new User(
Alias = 'tstusr',
Email = '[email protected]',
EmailEncodingKey = 'UTF-8',
LastName = 'Tester',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_US',
ProfileId = p.Id,
TimeZoneSidKey = 'America/Los_Angeles',
UserName = 'tstusr' + System.currentTimeMillis() + '@example.com'
);
insert u;
Boolean blocked = false;
System.runAs(u) {
try {
Database.query('SELECT Id, Internal_Notes__c FROM Account', AccessLevel.USER_MODE);
} catch (QueryException e) {
blocked = true;
}
}
Assert.isTrue(blocked, 'The query should fail when the user cannot read the field');
}
}The test inserts the user outside runAs, so setup runs with full access. Only the code under test runs as the restricted user.
Testing the allowed case
A good suite also proves the positive path. Create a permission set that grants read on the field, assign it with a PermissionSetAssignment, and repeat the query. Assert that no exception is thrown and that the field is returned. Without this test, a bug that blocks everyone would still pass the blocked-case test.
Common errors
- Testing as the admin. Without
runAs, the test sees every field and proves nothing. - Assuming a profile's access. The field's access depends on your org's configuration. Set it explicitly in the test, or verify it, so the test does not break when a profile changes.
- Mixing setup and test DML. Inserting a user and then other setup objects inside
runAscan raise a mixed DML error. Insert setup records before therunAsblock. - Asserting only on the exception type. Check the message or the data too, so a different failure does not pass by accident.
Exercise
A test wraps [SELECT Id, Internal_Notes__c FROM Account] in System.runAs(restrictedUser) and expects an exception, but none is thrown. Why, and how do you fix it?
Show answer
Inline SOQL runs in system mode, so FLS is ignored even inside runAs. Add WITH USER_MODE to the query, or use Database.query with AccessLevel.USER_MODE, so the restricted user's field access is enforced.