1

I am writing a test class to test SiteRegisterController.cls. But registerUser is returning null during testing. What could be the problem? Any thoughts?

1 Answer 1

2

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_sites.htm

The test class is clearly documented here.repeating for clarity

 // Test method for verifying the positive test case 

static testMethod void testRegistration() {
    SiteRegisterController controller = new SiteRegisterController();
    controller.username = '[email protected]';
    controller.email = '[email protected]';
    controller.communityNickname = 'test';
    // registerUser always returns null when the page isn't accessed as a guest user 

    System.assert(controller.registerUser() == null);
    controller.password = 'abcd1234';
    controller.confirmPassword = 'abcd123';
    System.assert(controller.registerUser() == null);
}

As mentioned we will get null when the page is not accessed as a Guest User.You may use System.Runas(GuestUser) to resolve your issue .

1
  • Could you please add details on how to create guest user in the above code? I mean what should be the user role, profile and user type for the guest user? I posted similar question here(salesforce.stackexchange.com/questions/96859/…), Could you please look into this if you have time? Appreciate your time. Thanks!
    – javanoob
    Commented Oct 22, 2015 at 23:25

You must log in to answer this question.

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