3

Say I have a class marked without sharing for reading eg Account records without regard to sharing settings. (This would be used in contexts where the current user's privilege level would disallow the necessary access, for example during the registration process for a Site.)

Is it then possible to write a unit test to assert that sharing is ignored?

I'm aware of System.runAs() but am struggling to understand how I could use this without first setting the organization-wide Sharing Settings to Private for just the unit test context.

3
  • I realise that you're asking this out of curiosity, but seems to me there's little benefit in testing things the platform's supposed to do for you :)
    – Matt Lacey
    Jul 11, 2014 at 5:39
  • 3
    Yeah, the spirit is to ensure that if someone (for example) changes the sharing keywords on an inner class, then tests immediately fail. Long story ;-) Jul 11, 2014 at 6:50
  • Oh, that sounds like you had some fun!
    – Matt Lacey
    Jul 12, 2014 at 3:25

2 Answers 2

3

You cannot test it without setting the org-wide defaults to Private for the object you are working on. Which makes sense because, you will get exactly the same behavior as when using your application manually.

1

Using runAs allows you to test for enforcement of sharing rules related to accessing object. If you are testing a class using the runAs, the same criteria would apply in regards to the without sharing.

The only time a runAs method would actually do something is if you create a record and assign ownership then use the runAs a different user and test of they have access to the record.

It does not enforce CRUD or FLS based on the profile.

So if you call a class inside the runAs and the class has without sharing set then the results would be as you would expect, the values would be returned as if the user had full access.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .