function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Allen2Allen2 

Hi!! please help me out to cover rest part of the code in test class.. I tried a lot but not able to cover that part...

APEX CLASS
public with sharing class ccController {
    public String firstName {get; set;}
    public String lastName {get; set;}
    public String email {get; set;}
    public String password {get; set {password = value == null ? value : value.trim(); } }
    public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
    public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }    
    public ccController() {}    
    private boolean isValidPassword() {
        return password == confirmPassword;
    }
    public PageReference registerUser() {    
           // it's okay if password is null - we'll send the user a random password in that case
        if (!isValidPassword()) {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
            ApexPages.addMessage(msg);
            return null;
        }    
        String profileId = null; // To be filled in by customer.
        String roleEnum = null; // To be filled in by customer.
        String accountId = ''; // To be filled in by customer.        
        String userName = email;
        User u = new User();
        u.Username = userName;
        u.Email = email;
        u.FirstName = firstName;
        u.LastName = lastName;
        u.CommunityNickname = communityNickname;
        u.ProfileId = profileId;        
        String userId = Site.createPortalUser(u, accountId, password);     
        if (userId != null) { 
            if (password != null && password.length() > 1) {
                return Site.login(userName, password, ApexPages.currentPage().getParameters().get('startURL'));
            }
            else {
                PageReference page = System.Page.ccPage;
                page.setRedirect(true);
                return page;

            }
        }
        return null;
    }
}

TEST CLASS

/**
* An apex page controller that supports self registration of users in communities that allow self registration
*/
@IsTest
public with sharing class ccControllerTEst {
    
    static testmethod void testCCController() {
        Account acc = new Account(Name = 'Test');
        insert acc;
        
        Profile p = [select id from profile where name='System Administrator'];
        User u = new User(alias = 'standt', email = 'standarduser@testorg.com', emailencodingkey = 'UTF-8', lastname = 'Testing', languagelocalekey = 'en_US',
                          localesidkey = 'en_US', profileid = p.Id, timezonesidkey = 'America/Los_Angeles',
                          username = 'testclassuser@testorg.com');
        insert u;
        
        ccController controller = new ccController();
        controller.firstName = 'FirstName';
        controller.lastName = 'LastName';
        controller.email = 'test@force.com';
        controller.communityNickname = 'test';
        // registerUser will always return 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); 
        //String userId = Site.createPortalUser(u, acc.Id, controller.password);
        
    } 
}
Abdul KhatriAbdul Khatri
I guess in Test context Site.Createportaluser will always return null. One suggestion is to put 
if(Test.isRunningTest()) userId = //default to any user like [SELECT Id FROM User WHERE IsActive = true LIMIT 1] 
just above where are you checking
if (userId != null) { 
            if (password != null && password.length() > 1) {
                return Site.login(userName, password, ApexPages.currentPage().getParameters().get('startURL'));
            }
            else {
                PageReference page = System.Page.ccPage;
                page.setRedirect(true);
                return page;
            }
        }

Let me know if it works

 
Allen2Allen2
Hi Abdul,

Thanks for your response but it's not working at all.