You need to sign in to do that
Don't have an account?
sumanth kumar 62
auto registration handler test class error
I have a test class which is as below
Code from Reg Handler Apex class:
@isTest 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'}); try{ 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); Test.startTest(); // 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'); Test.stopTest(); } }
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 have removed these methods but found that my code is covered to only 12%
Can anyone please help me out with this.
I see you are using seeAllData = true, Can you make it to seeAllData = false and check if the user creation is working or not.
Cheers!,
Durgapaavan
Thanks for your concern on my issue.
I have set seeAllData to false but that didn't work. I am getting the same error irrespective of seeAllData condition.
Could you please suggest how to resolve this.
Thanks,
Sumanth Kumar
I see that insert statement for user creation is commented in above code. Can you confirm that you used insert statement for the creation?.
Cheers,
Durgapaavan
No Durgapaavan. That method is working fine
The issue is with "testPDRegistrationLocaleLangCheckChineseTrad" method where it is throwing error in line 46. That means, temp is returning null value in goodLocaleLang.
Please suggest
Please find the createuser method below
I am recieving null from this method in the code. THis is working fine in real time scenario whereas in test class, it is returning null.
Also, using userinfo.getuserid() returns my id. But here I am inserting communtiy user. Is that the reason for null pointer exception. Please suggest