I would like to assert that User B cannot access a record that User A had created from a visualforce page.
The problem is I can't seem to throw the same exception a user would normally see when navigating through the UI.
My controller and test class seem straightforward to me.
Controller
public with sharing class SampleController {
public SampleController(ApexPages.StandardController stdController) {
...
}
}
Test Class
@isTest
public class TestClass {
static testMethod void test_sharing() {
Account account1;
system.runAs(getUserA()) {
account1 = new Account();
insert account1;
}
system.runAs(getUserB()) {
try {
SampleController controller
= new SampleController(
new ApexPages.StandardController(account1) );
system.assert(false, 'Did not throw');
}
catch (Exception e) {
system.assert(true);
}
}
}
}
SampleExtension
, notSampleController
. You can only have one controller at a time (but multiple extensions).