function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC9999SFDC9999 

Help with Test Class

Hi I need little bit help for Test My TaskListview Controller

 

public class TaskListViewController { 
     
    public String objectId {get; set;}
    public Integer numLeft {get; set;}
    public Integer total {get; set;}    
    private Integer startNdx = 0;
    private static Integer PAGESIZE = 8;
    private List<Task> fullTaskList = new List<Task>();
    private List<Task> displayedTaskList = new List<Task>();
    
    
    public TaskListViewController() {

    }
    
    public PageReference refreshPage() {
        return null;
    }
   
    public List<Task> getTasks() {
        String ownerId = UserInfo.getUserId();
        if (fullTaskList.isEmpty())
        {
            fullTaskList = [SELECT Id, WhatId, WhoId, ActivityDate, subject,Activity_Type__c ,status, priority, Description,  ReminderDateTime, IsReminderSet,isClosed 
                            FROM Task 
                            WHERE isClosed = false 
                            AND OwnerId = :ownerId];
            numLeft = fullTaskList.size();
            total = numLeft;
            if(numLeft <> 0)
                this.objectId = ((Task) fullTaskList[0]).id;
         }
        
        displayedTaskList.clear();
        if(numLeft <> 0)
        {
        Integer endNdx = startNdx + PAGESIZE;
        if (endNdx > total)
            endNdx = total;
            
        for (Integer i=startNdx; i<endNdx; i++)
            displayedTaskList.add(fullTaskList.get(i));
         }           
        return displayedTaskList;
    }
 
    private void updateTaskStatus() {
        System.debug('before : ' + fullTaskList);
        Integer i = 0;
        for (i=0; i<fullTaskList.size(); i++) {
            Task t = fullTaskList.get(i);
            if (this.objectId.equals(t.id)) {
                System.debug('updating status of ' + t);                                
                Task tmp = [SELECT Id, WhatId, WhoId, ActivityDate, subject, status, priority, Description, ReminderDateTime, IsReminderSet, isClosed 
                            FROM Task 
                            WHERE id = :t.id];
                fullTaskList.set(i, tmp);
                System.debug('updated to ' + tmp);
                //this.updatedItemStatus = tmp.status;
                break;
            }
        }
        
        System.debug('after : ' + fullTaskList);               
    }
    
    private void nextTask() {
        for (Task t : fullTaskList) {
            if (!t.isClosed) {
                System.debug('found non-closed object with id ' + t.id);
                this.objectId = t.id;
                break;
            }
        }
    }
  

    public void previous() {
        startNdx -= PAGESIZE;
    }
    
    public void next() {
        startNdx += PAGESIZE;
    }    
    
    public void refreshNumbers() {
        updateTaskStatus();
        nextTask();
        this.numLeft = 0;
        for (Task t : fullTaskList) {
            if (!t.isClosed) {
                this.numLeft++;
            }
        }        
      }
    
    public Boolean getHasNext() {
        return total > (startNdx + PAGESIZE);
    }
    
    public Boolean getHasPrevious() {
        return startNdx > 0;
    }    

    public Integer getNum() {
        return total;
    } 
    public PageReference Cancel() {
    PageReference Activitiespage = Page.Activities;
           Activitiespage.setRedirect(true);
           return Activitiespage;
        return null;
    }
    public PageReference save(){       
        for (Task t : fullTaskList) {
           update t;
           PageReference Activitiespage = Page.Activities;
           Activitiespage.setRedirect(true);
           return Activitiespage;
        }
        return null;    
    }
    }

 

@isTest
 Private Class TestTaskListViewController{
 
 static testMethod void testTaskListViewController() {
                
        TaskListViewController t = new TaskListViewController();        
        t.getTasks();    
        t.refreshPage();
        t.previous();
        t.next();
        t.refreshNumbers();
        t.getHasNext();
        t.getHasPrevious();
        t.save();
        t.getNum();
        t.Cancel();
        
                             
    }   
 }

 Getting Around 68% Coverage.

 

I am thinking that if i create a other task and update the status i think my update thing will cover, how would i cover previuos and next page and the page refrence.

 

Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
*rdinakaran**rdinakaran*
@isTest
Private Class TestTaskListViewController{

static testMethod void testTaskListViewController() {

TaskListViewController t = new TaskListViewController(); 
t.getTasks(); 
t.refreshPage();
t.previous();
t.next();
t.refreshNumbers();
t.getHasNext();
t.getHasPrevious();
t.save();
t.getNum();
t.Cancel();
//add the below code
Account Acct= new Account(Name = 'Test');
insert Acct;//Note:While giving this insert check the required fields for account and include it above

contact cont = new contact(Accountid = Acct.id,LastName = 'Test');
insert cont;//Note:While giving this insert check the required fields for contact and include it above

Task t1 = new Task(Subject = 'Test',Whoid = cont.id,Whatid = Acct.id,Status = 'Pending',Priority = 'Low',Description = 'Test',ownerid = UserInfo.getUserId());
insert t1;
TaskListViewController t2 = new TaskListViewController(); 
t2.getTasks(); 
t2.refreshNumbers() ;
t2.save();
t2.Cancel();
} 
}

 Use the above code , if this helps please make this as resolved ,and give me kudos.

 

Thanks,

All Answers

*rdinakaran**rdinakaran*
@isTest
Private Class TestTaskListViewController{

static testMethod void testTaskListViewController() {

TaskListViewController t = new TaskListViewController(); 
t.getTasks(); 
t.refreshPage();
t.previous();
t.next();
t.refreshNumbers();
t.getHasNext();
t.getHasPrevious();
t.save();
t.getNum();
t.Cancel();
//add the below code
Account Acct= new Account(Name = 'Test');
insert Acct;//Note:While giving this insert check the required fields for account and include it above

contact cont = new contact(Accountid = Acct.id,LastName = 'Test');
insert cont;//Note:While giving this insert check the required fields for contact and include it above

Task t1 = new Task(Subject = 'Test',Whoid = cont.id,Whatid = Acct.id,Status = 'Pending',Priority = 'Low',Description = 'Test',ownerid = UserInfo.getUserId());
insert t1;
TaskListViewController t2 = new TaskListViewController(); 
t2.getTasks(); 
t2.refreshNumbers() ;
t2.save();
t2.Cancel();
} 
}

 Use the above code , if this helps please make this as resolved ,and give me kudos.

 

Thanks,

This was selected as the best answer
Rajesh SriramuluRajesh Sriramulu

Hi,

 

Try This

 

@isTest
 Private Class TestTaskListViewController{
 
 static testMethod void testTaskListViewController() {
Account Acc= new Account(Name = 'Test');
insert Acc;

contact con= new contact(Accountid = Acc.id,LastName = 'Test');
insert con;
Task tas = new Task(Subject = 'Test',Whoid = con.id,Whatid = Acc.id,Status = 'Not Started',Priority = 'Normal',Description = 'Test');
insert tas;
                
        TaskListViewController t = new TaskListViewController();        
        t.getTasks();    
        t.refreshPage();
        t.previous();
        t.next();
        t.refreshNumbers();
        t.getHasNext();
        t.getHasPrevious();
        t.save();
        t.getNum();
        t.Cancel();
        
                             
    }   
 }

 

Regards,

Rajesh.

SFDC9999SFDC9999
Thanks Dude that worked like a charm , i was able to get 98% only return null line in Cancel method is not covered.
SFDC9999SFDC9999
Thanks Rajesh that worked perfectly.