+ Start a Discussion
uuuu 

Test class for apex class controller

Hello Developers,
Please help me to write test class for below apex contoller
public class HRApplyLeaveController {
    Public HRApplyLeaveController(){}
    public String loggesInName{get;set;}
    Public String ResourceName{get;set;}
    public Date FromDate{get;set;}
    
    public List<Leave__c> allLeave{set;get;}
    
    String loggesInId{set;get;}
    List<Resource__c> user = new List<Resource__c>();
    Leave__c lev;
    
    public HRApplyLeaveController(ApexPages.StandardController controller){
        
        ResourceName = System.currentPagereference().getParameters().get('name');
        lev = (Leave__c)controller.getRecord();
        
        user = [SELECT id,Name,Official_Email__c,Personal_Email__c from Resource__c where Name=:ResourceName];
        if(user.size()>0)
        {
            if(user[0].Name != NULL)
            {
                loggesInName =  user[0].Name;
            }
        }
        fetchallLeave();
    }
    //table
    public void fetchallLeave(){
        if(user.size()>0){
            allLeave=[Select From_Date__c,To_Date__c,Type__c,Decision__c,Number_of_Days__c From Leave__c 
                      where Resource__r.Name=:user[0].Name];
        }
    }
    //popup
     public boolean displayPopup {get; set;}
    public void closePopup()
    {       
        displayPopup = false;   
    }    
    public void showPopup()
    {       
        displayPopup = true;
        save();   
    }
    //insert record
    public Pagereference save(){
        lev.Resource__c=user[0].id;
        try{
            insert lev;
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Thank you!Record saved successfully'));
            
        }
        catch(Exception ex){
            ApexPages.addMessages(ex);
        }
        
        //send email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setSubject('Leave Application |'+user[0].Name);
        
        email.setToAddresses( new List<String>{user[0].Personal_Email__c} );
        email.setHtmlBody('Hii Sir/Mam,<br/><br/>https://login.my.salesforce.com/'+lev.Id+
                          '<br/><br/>Regards,<br/>'+'User');
        try{
            Messaging.SendEmailResult [] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        }catch(System.Exception e){
            system.debug(e);
        }
        return null;
    }
    //home button
    public Pagereference goHome(){
        PageReference pageRef = new PageReference('/apex/HRHome?id='+user[0].id);
        pageRef.setRedirect(true);
        return pageRef;
    }
}

Test class
@isTest
public class HRApplyLeaveControllerTest {
    static testMethod void Test_HRApplyLeaveController() 
    {
        Resource__c res= new Resource__c(Name='Swapnil Joshi',Official_Email__c='abc@xyz.com', Password__c='s123',Emp_ID__c=1);
        insert res;
        
        Leave__c l=new Leave__c();
        l.Resource__c=res.id;
        l.From_Date__c=system.today();
        l.To_Date__c=system.today()+5;
        insert l;
    }
}

Thanks in advance.. :)
Regards,
Anita sodhi
Best Answer chosen by uu
AnudeepAnudeep (Salesforce Developers) 
Hi Anita, 

You can get started with something like this
 
@isTest
public class HRApplyLeaveControllerTest {
    static testMethod void Test_HRApplyLeaveController() 
    {
        Resource__c res= new Resource__c(Name='Swapnil Joshi',Official_Email__c='abc@xyz.com', Password__c='s123',Emp_ID__c=1);
        insert res;
        
        Leave__c l=new Leave__c();
        l.Resource__c=res.id;
        l.From_Date__c=system.today();
        l.To_Date__c=system.today()+5;
        insert l;

        PageReference pageRef = Page.HRHome;
         Test.setCurrentPage(pageRef);

     pageRef.getParameters().put('Id', String.valueOf(res.Id));
     ApexPages.StandardController sc = new ApexPages.StandardController(res);
     HRApplyLeaveController hrleave = new HRApplyLeaveController(sc);
     hrleave.closePopup(); 
     hrleave.showPopup(); 
     hrleave.save(); 
     hrleave.goHome(); 
    }
}

Anudeep

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Anita, 

You can get started with something like this
 
@isTest
public class HRApplyLeaveControllerTest {
    static testMethod void Test_HRApplyLeaveController() 
    {
        Resource__c res= new Resource__c(Name='Swapnil Joshi',Official_Email__c='abc@xyz.com', Password__c='s123',Emp_ID__c=1);
        insert res;
        
        Leave__c l=new Leave__c();
        l.Resource__c=res.id;
        l.From_Date__c=system.today();
        l.To_Date__c=system.today()+5;
        insert l;

        PageReference pageRef = Page.HRHome;
         Test.setCurrentPage(pageRef);

     pageRef.getParameters().put('Id', String.valueOf(res.Id));
     ApexPages.StandardController sc = new ApexPages.StandardController(res);
     HRApplyLeaveController hrleave = new HRApplyLeaveController(sc);
     hrleave.closePopup(); 
     hrleave.showPopup(); 
     hrleave.save(); 
     hrleave.goHome(); 
    }
}

Anudeep
This was selected as the best answer
uuuu
Thank you so much Anudeep.
It works..:)
Regards,
Anita sodhi