+ Start a Discussion
Rakesh SRakesh S 

test class for page reference

Hi All,
this is my Controller:
public with sharing class LogACallController{

    public Task tsk{get;set;}
    public String WhoId{get;set;}
    public String WhatId{get;set;}
    public Task t{get;set;}
    Public ApexPages.StandardController con;
    public boolean isReminder{get;set;}
    public string RedirectId{get;set;}
    
    public LogACallController(ApexPages.StandardController controller) {
        
        this.con = controller;
        
        WhoId  = ApexPages.currentPage().getParameters().get('who_id');
        WhatId  = ApexPages.currentPage().getParameters().get('what_id');
        If(WhoId != null){
            tsk = new task();
            tsk.OwnerId = UserInfo.getUserId();
            //tsk.isReminderset=true;
            
       }else{
            tsk = new task();
            tsk.OwnerId = UserInfo.getUserId();
             // tsk.isReminderset=true;
        }
         //t = (Task)con.getRecord();
    }
    
    public PageReference save(){
      Task t = (Task)con.getRecord();
      tsk.Status = 'Completed';
      tsk.Date__c = t.Date__c;
      tsk.Subject = 'Call';
      tsk.isReminderset = isReminder;
      system.debug('XXXXXXX' + tsk);
      
   if(WhoId != null){
       // tsk.Status = 'Completed';
        
        tsk.Whoid=WhoId;
        insert tsk;
        this.RedirectId = WhoId;
        
        }
    else{
        //tsk.Status = 'Completed';
        tsk.WhatId=whatId;
        insert tsk;
        this.RedirectId = WhatId;
        
        }
        
        PageReference page = new PageReference('/'+RedirectId);
        page.setRedirect(true);
        return page;
    }
}

This is my Test Class:
@isTest
public class TestLogACall {
    static testmethod void SavetaskTestforContact1()
    {
        Contact c = new Contact();
        c.LastName = 'thamma';
        c.FirstName = 'harsha';
        insert c;
        Task tk = new Task();
        tk.OwnerId = UserInfo.getUserId();
        tk.WhoId = c.Id;
        ApexPages.currentPage().getParameters().put('who_id',c.id);
        ApexPages.StandardController sc = new ApexPages.StandardController(tk);
        LogACallController log = new LogACallController(sc);
        log.WhoId = c.Id;
        log.t = tk;
        log.RedirectId = c.id;
        log.save();
    }    
    static testmethod void SavetaskTestforwhat1()
    {
        physician__c p = new physician__c();
        p.Last_Name__c = 'ranga';
        p.First_Name__c = 'billa';
        p.Name ='dfed';
        insert p;
        Task tk = new Task();
        tk.OwnerId = UserInfo.getUserId();
        tk.WhatId= p.Id;
        ApexPages.currentPage().getParameters().put('what_id',p.id);
        ApexPages.StandardController sc = new ApexPages.StandardController(tk);
        LogACallController log = new LogACallController(sc);
        log.WhatId= p.Id;
        log.t = tk;
        log.RedirectId = p.Id;
        log.save();   
    }
}

this code not covered:
PageReference page = new PageReference('/'+RedirectId);
        page.setRedirect(true);
        return page;

Please help me to write test class.


Thank you..
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
@isTest
public class TestLogACall {
    static testmethod void SavetaskTestforContact1()
    {
        Contact c = new Contact();
        c.LastName = 'thamma';
        c.FirstName = 'harsha';
        insert c;

        Task tk = new Task();
        tk.OwnerId = UserInfo.getUserId();
        tk.WhoId = c.Id;
		tk.Status = 'Completed';
		tk.Subject = 'Call';		
		insert tk;
		
        ApexPages.currentPage().getParameters().put('who_id',c.id);
        ApexPages.StandardController sc = new ApexPages.StandardController(tk);
        LogACallController log = new LogACallController(sc);
        log.WhoId = c.Id;
        log.t = tk;
        log.RedirectId = c.id;
        log.save();
    }    
    static testmethod void SavetaskTestforwhat1()
    {
        physician__c p = new physician__c();
        p.Last_Name__c = 'ranga';
        p.First_Name__c = 'billa';
        p.Name ='dfed';
        insert p;

        Task tk = new Task();
        tk.OwnerId = UserInfo.getUserId();
        tk.WhatId = p.Id;
		tk.Status = 'Completed';
		tk.Subject = 'Call';		
		insert tk;
		
        ApexPages.currentPage().getParameters().put('what_id',p.id);
        ApexPages.StandardController sc = new ApexPages.StandardController(tk);
        LogACallController log = new LogACallController(sc);
        log.WhatId= p.Id;
        log.t = tk;
        log.RedirectId = p.Id;
        log.save();   
    }
}
Please let us know if this will help u

Thanks
Amit Chaudhary
Rakesh SRakesh S
Hi Amit Chaudhary,

the above code getting only 70 % of coverage.

the below code didnot covered.
PageReference page = new PageReference('/'+RedirectId);
        page.setRedirect(true);
        return page;


Thank you.