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?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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 = '<a href="" class="__cf_email__" data-cfemail="3f4b5a4c4b7f59504d5c5a115c5052">[email protected]</a>'; = '<a href="" class="__cf_email__" data-cfemail="245041575064424b5647410a474b49">[email protected]</a>';
    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 .

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x