I have a siteRegistrationController which is creating portal users as below:

Registration Controller:

public class SiteRegistrationController{

    public void createPortalUser(){
        //There is code to populate the values for variables u and acc.
        String userId = Site.createPortalUser(u, acc.Id,null,false);    
        System.debug('userId is '+userId);


I am trying to test the above code and below is in my test class:

Test Class:

public class SiteRegistrationController_TEST{

        SiteRegistrationController controllerObj = new SiteRegistrationController();

        User guestUserObj = new User(username='[email protected]',
                                Email='[email protected]',
        insert guestUserObj;


Site.createPortalUser is always returning null in test context.

  1. Similar question -- This post is also about same problem but no solution here.
  2. Similar question -- This post suggests that use the System.runAs(guestUser) but does not specify how to create guest user.

If any body has solution for this..Please help me. Thanks in Advance!

3 Answers 3


In my experience there is no way to avoid the null user ID return value from Site.createPortalUser in tests. Typically the user ID is not needed so this inconsistency doesn't undermine code coverage or other tests very much.

(If necessary, you can use Test.isRunningTest to add test-case specific logic.)


In order to create a portal user, you first need to create an Account and a contact. A guest or portal user has no Role, but they do have a Profile. You need to specify that profile when you create the User.

Below are snippets from old unit tests from when I was building a community that should work for you towards creating Sites Users, just substitute the right profiles, etc.


    /* Create Accts for Portal User Contacts to establish ownership & link to a role */

    for(i=0;i<(smpls +4);i++){
        Account CEact = new Account(Name= 'PAcctO'+ String.valueOf(i)+'', Is_Active__c = true, Region__c = 'SouthEast', OwnerID = U1.Id  );


    /* insert the new Accts */

    if(CEActs.IsEmpty() == false) Insert CEActs;

    /* Create Portal User Contacts */

    for(x=0;x<(smpls +4) ;x++)  {      

        Contact CECtc = new Contact( AccountID = CEActs[x].Id, FirstName = 'PrtlFrst0', LastName = 'PrtlLstO'+ String.valueOf(x)+'', Phone = '727-755-111'+string.valueOf(x)+'', MailingStreet = '11'+String.valueOf(x) +' Prtl0'+ String.valueOf(x)+' St', MailingCity = 'PortalCty0'+String.valueOf(x)+'', MailingState = 'FL', MobilePhone = '757-743-111'+String.valueOf(x)+'', Email='portalo'+string.valueOf(x)+'@tst.com' );

    /* insert the new Ctcs */

    if(CECtcs.IsEmpty() == false) Insert CECtcs;

    /* Create Portal Users */ 

    for(i=0;i<(smpls +4);i++){

        User u6 = new User(ContactID = CECtcs[i].Id, Alias = 'PrtlUsr'+(string.valueOf(i))+'', Email= CECtcs[i].Email, 
  EmailEncodingKey='UTF-8', FirstName= CECtcs[i].FirstName, LastName= CECtcs[i].LastName, TimeZoneSidKey='America/Chicago', 
  LanguageLocaleKey='en_US', LocaleSidKey='en_US', MobilePhone = CECtcs[i].MobilePhone, Phone = CECtcs[i].Phone,
  userName=CECtcs[i].Email, ProfileID = U5.ProfileID);


    if(PrtlUsrs.IsEmpty() == false) Insert PrtlUsrs;

    }// end System.RunAs(U0)
  • 1
    can you please specify what is u0 in your code? I mean what profile? Usertype? Role?
    – javanoob
    Commented Oct 22, 2015 at 22:03
  • Apologies for that oversight. U0 in that Org was the equivalent of a Sales Manager. It wasn't an Admin Profile which I presume is the essence of what you're wanting to know. It's really anyone who has sufficient access to create a community user through creating an account plus a related contact and then enabling them as an external user. Again, that was code written for a community, but a Sites user isn't much different.
    – crmprogdev
    Commented Oct 22, 2015 at 22:47
  • Thanks crmprogdev. But my question was on how to create the portal user using the createPortalUser method of Site class. I was able to create portal user like you mentioned in your code but I want to use the Site class to create portal user.
    – javanoob
    Commented Oct 22, 2015 at 23:22
  • Ahh, my apologies javanoob! I saw your method name that was identical and didn't see the callout within it that was referencing the site class.
    – crmprogdev
    Commented Oct 23, 2015 at 1:41

Use Test.isRunningTest() in your actual class

Similar to

if (Test.isRunningTest()){

userId = 'fakeUserId';


So that whenever the test class is running, it receives NULL from the Site.createPortalUser() method.

Using the above code will ensure that the User ID is not NULL, thereby code coverage is higher.

You must log in to answer this question.

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