+ Start a Discussion
ezhil_kezhil_k 

how to write test class for standard controller extension for the below code? Please help me..

public class newuser {

    public HR__c rec {set;get;}
    public HR__c hr {set;get;}
    public User myNewUser {set;get;}    
          
       
    public newuser(ApexPages.StandardController controller) {
            myNewUser = new User();
            rec = (HR__c) controller.getRecord();
            hr  = [Select Id, Job_Code__c, Job_Title__c,Employee_Number__c, User__c From HR__c where Id =: rec .Id];
            myNewUser.EmployeeNumber = hr.Employee_Number__c;
            myNewUser.Title= hr.Job_Title__c;
            myNewUser.JobCode__c=hr.Job_Code__c;                 
   }
    
    public PageReference saveUser(){
    
            insert myNewUser;
            hr.User__c = myNewUser.Id;
            update hr;
            PageReference pageRef= new PageReference('/'+ hr.Id);
            return pageRef ;           
         }
    }

sambasamba

HR__c hr = new HR__c();

ApexPages.StandardController stdHR = new ApexPages.StandardController(hr);

newuser testnewUser = new newuser(stdHR);

 

I think you already know. 

 

Thanks,

Samba

asish1989asish1989

Try this

@isTest
public class Testnewuser
{
  static testMethod void myTest()
  {
    
    HR__c testHR = new HR__c();
	testHR.Job_Code__c = 'test';
	testHR.Job_Title__c = 'title';
	testHR.Employee_Number__c = 'empno';
	insert testHR;
	
	List<Profile> ps = [select id, name from Profile where  name = 'System Administrator'];
	
	User u = new User(alias = 'test123', email='test123@noemail.com',
		          emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
			  localesidkey='en_US', profileid = 'ps[0].Id', country='United States',
			  timezonesidkey='America/Los_Angeles', username='test123@noemail.com');
			 u.Title = testHR.Job_Title__c;
                         u.JobCode__c = testHR.Job_Code__c;	
			 u.EmployeeNumber = testHR.Employee_Number__c;			
                        insert u;
			testHR.User__c = u.id;
			update testHR;
			Test.StartTest();
			ApexPages.StandardController stdHR = new ApexPages.StandardController(testHR);

            newuser testnewUser = new newuser(testHR);
			testnewUser.saveUser();
			Test.StopTest();
	
  }
 } 

 

ezhil_kezhil_k

Hi ,

Am getting error like this"System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing:'

 

 

@istest

Public Class  newuserTest {
    public static testmethod void test(){
    
     HR__c hrRec=(new HR__( Name='kalai',Employee_Number__c='11115',Job_Code__c='ASC05', Job_Title__c='ASC' )); 
     insert hrRec;

     List<Profile> ps = [select id, name from Profile where  name = 'Standard Platform User'];
    
     User u=new User(FirstName='shiva',LastName='kumar' ,Alias='sk', Username='shiva@ymail.com', Email='sk@gmail.com', ProfileId=ps[0].Id,TimeZoneSidKey='Asia/Colombo',LocaleSidKey='hi_IN', EmailEncodingKey='ISO-8859-1', LanguageLocaleKey='en_US',CommunityNickname='test');  
     u.Title=hrRec.Job_Title__c;
     u.JobCode__c=hrRec.Job_Code__c;
     u.EmployeeNumber=hrRec.Employee_Number__c; 
     insert u;
      
     hrRec.User__c=u.Id;
     update hrRec;    
     
     ApexPages.StandardController controller = new ApexPages.StandardController(hrRec);
     newUser cont= new newUser(controller);
     cont.saveUser();
       }
    
    }

asish1989asish1989

Please make sure that all the required field of HR__c and User object is entered in test class,

please find out  what are the required field of both Object and bind all filed with some value before Inserting.

ezhil_kezhil_k

values for all the required fields are given..but still its showing the error;

 

Error  message:"System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Username, LastName, Email, Alias, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey]: [Username, LastName, Email, Alias, CommunityNickname, TimeZoneSidKey, LocaleSidKey, EmailEncodingKey, ProfileId, LanguageLocaleKey"

 

stack trace:

Class.newUser.saveUser: line 19, column 1
Class.newuserTest.test: line 22, column 1

Michael_TorchedloMichael_Torchedlo

The error is not coming from the test code, it is coming from your regular class.  The code you posted creates a new User that has no name, no email, and none of the other required fields, so when it tries to insert the record it will fail.

 

 

 

public class newuser {

    public HR__c rec {set;get;}
    public HR__c hr {set;get;}
    public User myNewUser {set;get;}    
          
       
    public newuser(ApexPages.StandardController controller) {
            myNewUser = new User();  //you need to populate the required fields in this method before you try to save
            rec = (HR__c) controller.getRecord();
            hr  = [Select Id, Job_Code__c, Job_Title__c,Employee_Number__c, User__c From HR__c where Id =: rec .Id];
            myNewUser.EmployeeNumber = hr.Employee_Number__c;
            myNewUser.Title= hr.Job_Title__c;
            myNewUser.JobCode__c=hr.Job_Code__c;                 
   }
    
    public PageReference saveUser(){
    
            insert myNewUser;
            hr.User__c = myNewUser.Id;
            update hr;
            PageReference pageRef= new PageReference('/'+ hr.Id);
            return pageRef ;           
         }
    }

yarramyarram
HI  ezhil_k,
did you resolved above mentioned required fields missing error? 
I am also getting same type of error in userSave() method. if you resolved this please guide me how to solve this error.

it's very urgent for me. please reply ASAP.

Thanks,
Yarram