Find your content:

Search form

You are here

Does Site.createPortalUser work in test class?


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

Attribution to: doga

Possible Suggestion/Solution #1

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 = ''; = '';
    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 .

Attribution to: Mohith Shrivastava
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content