+ Start a Discussion
Kelly KKelly K 

Testing Portal User Creation with a Sharing Handler

Hi All,

Is there a way to get a successful test for an @future method called from creating/updating a user?

Here's the scenario - when we enable a customer portal user, there are several items (such as an account heirarchy) that need to be shared through apex in order to ensure that the user has access to relevant records. The @future method gets fired when a user is created or when a user is reactivated. 

The problem is, updating sharing is considered a non-setup item and updating a user is considered a set-up item, so I end up with Mixed_DML errors galore.

I ended up settling on something like this:

static testMethod void userActivateTest() {
    	User user = [SELECT Id, FirstName, LastName, ContactId, AccountId, IsActive FROM User WHERE  IsActive=true AND IsPortalEnabled=true LIMIT 1];

    	List<User> userList = new List<User>();
    	userList.add(user);

    	Test.startTest();
    		CP_SharingHandler.user_Add_Shares_Prep(userList);
    	Test.stopTest();
    }

While it does generate the code coverage I need, I feel like this is sloppy coding since I cannot use any assert statements to ensure that the @future method is doing what it actually needs to do.

Has anyone had any success in testing shares being generated when setting up a portal user? If so, what approach did you take?

Thanks,
Kelly