I have a test class which is as below

private class KC_Reg_Handler_Test {
    static testMethod void testCreateAndUpdateUser() {

        KC_RegHandler handler = new KC_RegHandler ();
        Auth.UserData sampleData = new Auth.UserData('testId', 'testFirst', 'testLast',
                                                     'testFirst testLast', 'testuse8888r@example.org', null, 'testuserlong', 'en_US', 'facebook',
                                                     null, new Map<String, String>{'language' => 'en_US'});

           user  u = handler.createUser(null, sampleData);

            // insert(u);
            //String uid = u.id;

            sampleData = new Auth.UserData('testNewId', 'testNewFirst', 'testNewLast',
                                           'testNewFirst testNewLast', 'testnewuser@example.org', null, 'testnewuserlong', 'en_US', 'facebook',
                                           null, new Map<String, String>{});
            handler.updateUser(null, null, sampleData);
        }catch(Exception e){

        // User updatedUser = [SELECT userName, email, firstName, lastName, alias FROM user WHERE id=:uid];


     @isTest (seeAllData=true)
static void testPDRegistrationLocaleLangCheckChineseTrad() {
    Account acct = testDataGenerator.createTestAccount();                 
    Contact cont = testDataGenerator.createTestContact();

    String uniqueName = 'xyz@newtest.666555444.com';
    Auth.UserData dataX= new Auth.UserData('goodidentifier', 'firstName', 'lastName', 'fullName', 'cat@newtest.com', 
        null, uniqueName, 'en', 'provider', null, null);        

    // PD Auth Provider Testing
    kc_RegHandler authP = new kc_RegHandler();    

    acct.GoogleId__c = 'goodidentifier';      
    String AccReq = '{"timezone":null,"strId":"","locale":"es_EC","lastName":"","language":"zh_HK","googleId":"0","gmailAddress":"0@newtest.com","firstName":"","emailAddress":"0@newtest.com","customId":"0","alias":null}';
    acct.Account_Request__c = AccReq;
    acct.Account_Status__c = 'Active';
    update acct;
    User temp = authP.createUser(UserInfo.getUserId(), dataX);        
    User goodLocaleLang = [SELECT Id, languagelocalekey, localesidkey, Contact.Account.Account_Request__c FROM User WHERE Id=:temp.Id LIMIT 1];        
    System.assertEquals(goodLocaleLang.Contact.Account.Account_Request__c, AccReq);
    System.assertEquals(goodLocaleLang.languagelocalekey, 'zh_CN');
    System.assertEquals(goodLocaleLang.localesidkey, 'es_EC');


Code from Reg Handler Apex class:

 global boolean canCreateUser(Auth.UserData data) {
        //TODO: Check whether we want to allow creation of a user with this data
        /*Set<String> s = new Set<String>{'brian@foreverliving.com.dev'};
        if(s.contains(data.username)) {
            return true;
        return false;*/
        return true;

    global User createUser(Id portalId, Auth.UserData data){
        if(!canCreateUser(data)) {
            //Returning null or throwing an exception fails the SSO flow
            return null;

in the above code, I found that the user is not getting inserted and after running the test class, I am recieving null pointer exception. That means the user is not getting created/inserted at authP.CreateUser and auth.CanCreateUser These methods are creating users and when I have checked this null pointer, my code is not getting covered completely. I found a similar post here but there is no solution mentioned in the comments. I removed these methods as suggested by @David Reed in the above link, but it didn't work. Can anyone help me out with this? Thanks


I'm afraid you mistook my meaning in the linked answer. You don't need to remove the methods createUser() and canCreateUser(); you need to implement them, using the logic that is appropriate for your specific auto-registration flow.

At minimum, to allow your code to complete execution, canCreateUser() must return true, which you've hard-coded it to do here, and createUser() must return a User record. createUser(), however, is now hardwired to do nothing at all. (It shouldn't compile as shown here).

You need to simply write the code in that method to create the User record based on the inbound Auth.UserData information.

  • Thanks @David for your comment. As per our business logic, we do not have any issues with those blocks and our authentication is working fine with the controller we have. Whereas, in the test class, it is throwing error. Am I missing anything here? – Skumar Aug 5 at 11:35
  • @Skumar I don't know. I think you would need to edit your question to show the exception line number and full stack trace, and include the code involved - there must be more code in createUser(), since what you posted won't compile. – David Reed Aug 5 at 11:36
  • Thanks @David, Correct me if I'm wrong here. I can see that the issue is with UserInfo.getuserId() as it has returned null when I have executed the code in anonymous block. As we are running this code for partner user, userinfo.getuserId returns my user id. May be, that was the reason why it is throwing null pointer exception. How to overcome this? Please suggest – Skumar Aug 5 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.